ryanb / nested_form

Rails plugin to conveniently handle multiple models in a single form.
MIT License
1.79k stars 505 forks source link

Wrong behaviour of link_to_remove in a deep nested form #282

Closed danielbecker closed 10 years ago

danielbecker commented 10 years ago

I created a rather complex form with 4 nesting levels. Everything works fine except for the deepest link_to_remove. When I click the link, it removes not only all the forms of this nesting level, which it shouldn't, but all records for one level above, too. I have attached an error case which hopefully is understandable.

That's my form:

<%= simple_nested_form_for @order do |f| %> <%= f.simple_fields_for :order_positions do |g|%> <%= g.simple_fields_for :component_charts do |h| %> <%= h.simple_fields_for :component_chart_positions, wrapper: false do |i| %> <% end %>
Test 1 Test 2
<%= i.input :amount %> <%= i.link_to_remove "remove_position" %>
<%= h.link_to_add "add_position", :component_chart_positions %>

<%= h.link_to_remove "Remove component chart" %>

<% end %> <%= g.link_to_add "componen charts add", :component_charts %> <% end %> <% end %>

And here is the corresponding JS:

window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) { switch (assoc){ case "component_charts": var $destination = $(link).closest('.fields'); var $new_content = $(content).insertBefore($destination); break; case "component_chart_positions": var $destination = $(link).closest('tr'); var $new_content = $(content).insertAfter($destination); break; } return $new_content; }

I did not add any more code regarding the form. I hope could either help me understand what I'm doing wrong or fix it :)

Thanks!