如何在Rails 4中呈现部分表单?

时间:2021-01-19 20:16:32

undefined local variable or method `f' for #<#:0x000001080edfe0>

未定义的局部变量或方法`f'用于#<#:0x000001080edfe0>

I am trying to render a form within a template page with:

我试图在模板页面中呈现一个表单:

<%= form_for @vehicle, html: { class: "f_grp" }, remote: true do |f| %>
  <%= render "vehicles", locals: { f: f } %>
<% end %>

The file is loading. But I'm getting an undefined method on f error. Any ideas?

文件正在加载。但是我在f错误上得到一个未定义的方法。有任何想法吗?

1 个解决方案

#1


24  

You only use :locals when you use :partial.

您只能在使用时使用:locals:partial。

Either of these are correct:

这些都是正确的:

<%= render partial: 'vehicles', locals: { f: f } %>

Or (as of Rails 2.3):

或者(从Rails 2.3开始):

<%= render 'vehicles', f: f %>

Your version, which combines both, creates a local called locals and sets its value to a hash containing f: FormBuilder....

你的版本结合了两者,创建一个本地调用的本地,并将其值设置为包含f:FormBuilder的哈希....

It's worth noting that the only reason to still use locals: is when you use render :collection.

值得注意的是,仍然使用本地的唯一原因是:当你使用render:collection时。

#1


24  

You only use :locals when you use :partial.

您只能在使用时使用:locals:partial。

Either of these are correct:

这些都是正确的:

<%= render partial: 'vehicles', locals: { f: f } %>

Or (as of Rails 2.3):

或者(从Rails 2.3开始):

<%= render 'vehicles', f: f %>

Your version, which combines both, creates a local called locals and sets its value to a hash containing f: FormBuilder....

你的版本结合了两者,创建一个本地调用的本地,并将其值设置为包含f:FormBuilder的哈希....

It's worth noting that the only reason to still use locals: is when you use render :collection.

值得注意的是,仍然使用本地的唯一原因是:当你使用render:collection时。