ryanb / nested_form

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

Allow nested associations with same name (fixes #237) #238

Open groe opened 11 years ago

groe commented 11 years ago

This fixes #237 by using more specific regular expressions for nested associations.

Example Think of this model: A has_many B. B has_many B.

Right now if you are in the form of A and click the add link, the blueprint will be loaded and every occurence of the regexp /(_b_attributes)_.+?_/g will be replaced with $1_0_ and every occurence of /(\[b_attributes\])\[.+?\]/g will be replaced with $1[0]. Thats okay for the first nesting level (as this contains simple names as a[b_attributes][new_b][_destroy]). However, in the second level (e.g. name = a[b_attributes][new_b][b_attributes][new_b][_destroy]) this will not work anymore, as the first replace will catch both occurences of both associations since they have the same name.

My patch is to make the regexps more specific by prepending the full path of the parent associations.

This pull request does not entirely fix support for recursive associations, but it allows users to fix it on their own, see #237 on how to. Of course, it would be awesome if even that wouldn't be necessary some day.