rails fields_for render partial with multiple locals producing undefined variable
Asked Answered
F

3

11

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.

Fivefold answered 22/11, 2010 at 18:40 Comment(3)
Are you sure you aren't rendering comment_fields in more than one place? If you are, can you paste comment_fields?Ermey
Unfortunately, I'm only rendering comment_fields from the _form partial. The problem only occurs when I try to pass more than one local variable.Fivefold
hakunin - you were right on this one. I was actually calling it from two places. The second place was within some methods which I use to dynamically add fields using javascript.Fivefold
F
17

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.

Fivefold answered 22/11, 2010 at 20:21 Comment(4)
Amazing, I also had a link_to_add_fields helper method that was causing this exact problem! Thanks for posting your answer!Berwick
That's great never thought that the link_to_add_fields is causing the error :DMighell
klass.new should now be build_association apidock.com/rails/ActiveRecord/Reflection/AssociationReflection/…Julissajulita
Sorry, reading further: apidock.com/rails/ActiveRecord/Reflection/AbstractReflection/…Julissajulita
M
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.

Megaera answered 24/11, 2011 at 16:12 Comment(1)
Thanks, but I don't think that was my original issue way back then... I also think the two statements you have above are equivalent because render without arguments defaults to using a partial... guides.rubyonrails.org/…Fivefold
H
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.

I strongly believe that

<%= tester.to_s %>
should not generate any issue as it only displays a value of that variable
Hausmann answered 22/11, 2010 at 18:47 Comment(1)
The tester variable is just that... a test. In the above, a comment belongs to an Article. The article belongs to an Account. What I really want to do is pass in the Article's account. My locals actually looks something like: :locals => {:f => cf, :account => article.account}. Unfortunately, this was not working and I was receiving the "undefined variable" error. As part of my debugging, I simplified with this "tester" variable. As you have mentioned, it should not generate an issue. This is the problem.Fivefold

© 2022 - 2024 — McMap. All rights reserved.