我有一个帮助器,我用来生成一个表单.用于生成表单字段的参数将传递给帮助程序.我无法弄清楚如何在模板外使用块.
例如:
- 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
解决方法
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