加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何在帮助程序中使用form_tag?

发布时间:2020-12-17 04:24:20 所属栏目:百科 来源:网络整理
导读:我有一个帮助器,我用来生成一个表单.用于生成表单字段的参数将传递给帮助程序.我无法弄清楚如何在模板外使用块. 例如: def generate_form(path,fields) form_tag(path,method: :get) do # what do I do in here? endend 当我在块中渲染部分时,渲染的网页中
我有一个帮助器,我用来生成一个表单.用于生成表单字段的参数将传递给帮助程序.我无法弄清楚如何在模板外使用块.

例如:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读