Rails 4 个带有表单的自定义辅助方法未在视图中获得输出 - 完全

Rails 4 custom helper method with form doesn't get output in view - fully

我在 application_helper.rb 中有这个,因为我对许多模型使用相同的过滤器形式。

def filter_form(path, filter_options, button)
    form_tag(path, remote: true, method: "get") do 
      label_tag(:filter, "Filter by")
      select_tag(:filter_type, options_for_select(filter_options))
      "<span>for:</span>".html_safe
      text_field_tag(:filter)
      button_tag(:submit, "Submit") if button == true
    end
  end

例如在我的用户文章中我有

<%= filter_form(articles_path, @filter_options, false) %>

但是在代码中,我可以看到它只生成 <form action="/articles" accept-charset="UTF-8" data-remote="true" method="get"><input name="utf8" value="✓" type="hidden"></form> 标签,这一切都很好,但是 none 的表单元素出现了。这是为什么?

尝试使用 concat

def filter_form(path, filter_options, button)
   form_tag(path, remote: true, method: "get") do 
     concat label_tag(:filter, "Filter by")
     concat select_tag(:filter_type, options_for_select(filter_options))
     "<span>for:</span>".html_safe
     concat text_field_tag(:filter)
     concat button_tag(:submit, "Submit") if button == true
   end
end

有一篇好文章here