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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…