如何将表单变量传递给页脚?

时间:2018-02-18 05:15:50

标签: ruby-on-rails ruby-on-rails-5

我想将我的表单的= f.submit部分放在页脚部分中,但是我收到了错误:

  

响应中的NameError#new

     

未定义的局部变量或#<#Class<Class:0x00007fcd80439d10>:0x00007fcd804314d0>

的方法'f'

您在下面的代码示例中会推荐哪些更改?

应用程序/视图/布局/ application.html.haml

%body
  %main
    = yield
  %footer
    = render partial: 'layouts/footer', locals: { f: f } # <-- this line errors

应用程序/视图/响应/ new.html.haml

= form_for @response do |f|
  = f.text_field :answer

应用程序/视图/布局/ _footer.html.haml

= f.submit

1 个答案:

答案 0 :(得分:0)

如果你想分享这样的元素,可以使用content_foryield,如下所示:

# _footer.haml
= yield :footer_content

然后在你的模板文件中:

# new.haml
= form_for @object, id: 'form1' do |f|
  # form elements goes here

= content_for :footer_content do 
  %button{:onclick => "document.getElementById('form1').submit()"}

由于按钮位于表单之外,您需要添加该自定义JS以正确提交表单。

希望这有帮助。