【问题标题】:Rails form_tag only returns last inputRails form_tag 只返回最后一个输入
【发布时间】:2019-08-26 08:37:05
【问题描述】:

我正在使用 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 返回包含所有输入的表单,而不仅仅是最后一个?

【问题讨论】:

    标签: html ruby-on-rails ruby forms


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 2023-01-15
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多