如何在帮助程序中使用form_tag?

时间:2012-09-29 18:15:17

标签: ruby-on-rails

我有一个帮助我用来生成表单。用于生成表单字段的参数将传递给帮助程序。我无法弄清楚如何在模板外使用块。

例如:

def generate_form(path, fields)
    form_tag(path, method: :get) do
        # what do I do in here?
    end
end

当我在块中渲染部分时,渲染的网页中不会出现任何内容。如果我将一堆标签(field_tag,text_field_tag等)连接在一起,那么页面上会出现原始html。

我正在使用Rails 3.1.0

1 个答案:

答案 0 :(得分:11)

Rails元素助手返回字符串,因此你可以这样做:

def generate_form(path, fields)
  s = form_tag(path, method: :get) do
    p = input_tag
    p << submit_tag #(everything will be wrapped in form tag)
    p #returns p from block
  end
  s.html_safe #returns s and avoids html escaping
end
相关问题