Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

rails fields_for render partial with multiple locals producing undefined variable

All,

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

<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:

<%= 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>)

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


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

<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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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:

# 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:

<%= 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...