rails fields_for使用多个本地生成部分生成未定义变量的部分

时间:2022-10-08 17:19:56

All,

I am experiencing a problem with a standard fields_for setup. In my "_form" partial I have:

我遇到了标准fields_for设置的问题。在我的“_form”部分我有:

<div class="comment_list">
  <%= f.fields_for :comments do |cf| %>
    <%= render :partial => 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>
  <% end %>

  <%= link_to_add_fields "Add a comment", f, :comments %>
</div>

In the "_comment_fields" partial, I have the usual fields and then my test variable:

在“_comment_fields”部分中,我有通常的字段,然后是我的测试变量:

<%= tester.to_s %>

When I remove the tester variable, everything works well. As soon as I add the test variable, I get this error:

当我删除测试器变量时,一切都运行良好。一旦我添加测试变量,我就会收到此错误:

ActionView::Template::Error (undefined local variable or method `tester' for #Class:0xa1f3664>:0xa1f1bd4>)

ActionView :: Template :: Error(#Class的未定义局部变量或方法`tester':0xa1f3664>:0xa1f1bd4>)

Has anyone else ran into this problem when using a fields_for with multiple locals?

有没有其他人在使用具有多个本地的fields_for时遇到此问题?


To elaborate a bit more, my "_comment_fields" partial looks like this:

为了详细说明,我的“_comment_fields”部分看起来像这样:

<div class="comment dynamic_field">
  <span class="comment_content"><%= f.text_field :content, :class => "comment_content" %></span>
  <%= tester.to_s %>
  <%= link_to_remove_fields "remove", f %>
</div>

It is only called from the "_form" partial.

它仅从“_form”部分调用。

3 个解决方案

#1


15  

All,

Hakunin was on the money. I was calling the partial in more than one spot. The second spot was in my helper method "link_to_add_fields." I use this to add fields using javascript.

Hakunin赚钱了。我在不止一个地方打电话给我。第二个点是我的帮助方法“link_to_add_fields”。我用它来使用javascript添加字段。

The method looked like this:

方法看起来像这样:

# generates add fields on a dynamic form
def link_to_add_fields(name, f, association, locals={})  
  new_object = f.object.class.reflect_on_association(association).klass.new  
  fields = f.fields_for(association, new_object, 
                      :child_index => "new_#{association}") do |builder|  
    render(association.to_s.singularize + "_fields", :f => builder)  
  end  

  link_to(name, "#", :class => "dynamic_add", 'data-association' => "#{association}",
                                            'data-content' => "#{fields}")
end  

Notice that this does not allow any locals to be passed to the render method. I changed it like so:

请注意,这不允许将任何本地传递给render方法。我这样改了:

# generates add fields on a dynamic form
def link_to_add_fields(name, f, association, locals={})  
  new_object = f.object.class.reflect_on_association(association).klass.new  
  fields = f.fields_for(association, new_object, 
                      :child_index => "new_#{association}") do |builder|  
    render(association.to_s.singularize + "_fields", locals.merge!(:f => builder))  
  end  

  link_to(name, "#", :class => "dynamic_add", 'data-association' => "#{association}",
                                            'data-content' => "#{fields}")
end  

Now my link_to_add_fields call in my _form partial looks like this:

现在我的_form部分中的link_to_add_fields调用如下所示:

<%= link_to_add_fields "Add a comment", f, :comments, :tester => true %>

...and I can dynamically add fields to my form AND pass additional locals. Hopefully, this will help someone else out.

...我可以动态地向表单添加字段并传递其他本地。希望,这将有助于其他人。

#2


1  

Change :

<%= render :partial => 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>

to :

<%= render 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>

I just had the same problem.

我刚遇到同样的问题。

#3


0  

I am not clear why do you need to use tester variable in form field. But can you please paste a code how are you using tester variable in partial form.

我不清楚为什么你需要在表单字段中使用tester变量。但是你可以粘贴一个代码,你如何使用部分形式的测试变量。

I strongly believe that

我坚信这一点

<%= tester.to_s %>
should not generate any issue as it only displays a value of that variable

#1


15  

All,

Hakunin was on the money. I was calling the partial in more than one spot. The second spot was in my helper method "link_to_add_fields." I use this to add fields using javascript.

Hakunin赚钱了。我在不止一个地方打电话给我。第二个点是我的帮助方法“link_to_add_fields”。我用它来使用javascript添加字段。

The method looked like this:

方法看起来像这样:

# generates add fields on a dynamic form
def link_to_add_fields(name, f, association, locals={})  
  new_object = f.object.class.reflect_on_association(association).klass.new  
  fields = f.fields_for(association, new_object, 
                      :child_index => "new_#{association}") do |builder|  
    render(association.to_s.singularize + "_fields", :f => builder)  
  end  

  link_to(name, "#", :class => "dynamic_add", 'data-association' => "#{association}",
                                            'data-content' => "#{fields}")
end  

Notice that this does not allow any locals to be passed to the render method. I changed it like so:

请注意,这不允许将任何本地传递给render方法。我这样改了:

# generates add fields on a dynamic form
def link_to_add_fields(name, f, association, locals={})  
  new_object = f.object.class.reflect_on_association(association).klass.new  
  fields = f.fields_for(association, new_object, 
                      :child_index => "new_#{association}") do |builder|  
    render(association.to_s.singularize + "_fields", locals.merge!(:f => builder))  
  end  

  link_to(name, "#", :class => "dynamic_add", 'data-association' => "#{association}",
                                            'data-content' => "#{fields}")
end  

Now my link_to_add_fields call in my _form partial looks like this:

现在我的_form部分中的link_to_add_fields调用如下所示:

<%= link_to_add_fields "Add a comment", f, :comments, :tester => true %>

...and I can dynamically add fields to my form AND pass additional locals. Hopefully, this will help someone else out.

...我可以动态地向表单添加字段并传递其他本地。希望,这将有助于其他人。

#2


1  

Change :

<%= render :partial => 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>

to :

<%= render 'comments/comment_fields', :locals => {:f => cf, :tester => true} %>

I just had the same problem.

我刚遇到同样的问题。

#3


0  

I am not clear why do you need to use tester variable in form field. But can you please paste a code how are you using tester variable in partial form.

我不清楚为什么你需要在表单字段中使用tester变量。但是你可以粘贴一个代码,你如何使用部分形式的测试变量。

I strongly believe that

我坚信这一点

<%= tester.to_s %>
should not generate any issue as it only displays a value of that variable