Rails form_tag仅返回最后一个输入

时间:2019-04-04 14:27:15

标签: html ruby-on-rails ruby forms

我正在使用Rails form_tag帮助器为页面创建一个特定的帮助器。这是我的代码:

  def horta_form(form, args = {})
    set_editable(form) unless args[:editable] == false
    identifier, send_to, classes, style = form.identifier, form.send_to, form.classes, form.style
    form_tag("/contact", method: "post") do
      text_field_tag('name')
      text_field_tag('email')
      text_area_tag('message')
      submit_tag('Send')
    end
  end

但是在我看来,它仅呈现最后一个标签submit_tag

<%= horta_form(@form) %>

返回

<form action="/contact" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="✓">
  <input type="hidden" name="authenticity_token" value="/WLo9GzhGdD7dBk3Eh8k4Q/+jQ0r+EGqgoOXBedyl/NW6g5gBQ/R4U4gFEtXmz1xJlISHAYykZRkmDHhQJm8uQ==">
  <input type="submit" name="commit" value="Send" data-disable-with="Send">
</form>

我该怎么做才能使horta_form返回具有所有输入的表格,而不仅仅是最后一个?

1 个答案:

答案 0 :(得分:1)

当您的助手中需要嵌套结构时,请使用concat

def horta_form(form, args = {})
  #...
  form_tag("/contact", method: "post") do
    concat text_field_tag('name')
    concat text_field_tag('email')
    concat text_area_tag('message')
    concat submit_tag('Send')
  end
end