ryanb / nested_form

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

Adding a new nested field in a newly added nested field #343

Closed Startouf closed 9 years ago

Startouf commented 9 years ago

So I have two levels of nesting Project has_many :phases Phase has_many :affectation

Problem is that when I dynamically add a new phase, and inside I add a new affectation using link_to_add '', :phases and link_to_add '', :affectations, well my nested affectation cannot resolve the ID of the Phase anymore. Thus every element generated by the nested form will generate a name like:

etude[phases_attributes][new_phases][affectations_attributes][1415654637807][intervenant_id]

The problem being that [new_phases] isn't replaced by the proper generated ID

I am using tables and nested tables, but I then I tagged the different tables with an ID:

_phase_fields.html.erb

<table id="<%= phase.index %>" class="phase-table"
...
    <tbody><%= phase.fields_for :affectations, :wrapper => false %></tbody>
...
<tr> <%= phase.link_to_add 'Add affectation', :affectations, :data => { :target => "##{phase.index}"}

So it shouldn't be a problem right ? So how come [new_phases] isn't substituted ? I'll try to look at the source code of the gem but I'm not sure I'm goog enough to track the bug. Or maybe I just forgot something else in my code ?

Startouf commented 9 years ago

Actually, [new_phases] is substituted if one phase has been saved in the DB, and the view loads it. But then, whatever the number of phase for the current Project, [new_phases] is always substituted by [0], which is exactly the same as described in issue https://github.com/ryanb/nested_form/issues/258

Startouf commented 9 years ago

Ok so my problem is that I didn't realise that the class "fields" was at the core of the gem. And in my case, I had some stupid CSS style .field {display: table-caption} coming from God knows where, that screwed up everything when I tried to use it.

Because my __xxxfields.html.erb partials included more than one <tr>, I couldn't just tag the first one with this class, but rather I had to wrap them inside a <tbody class="fields">.

But anyway, it should be mentionned on the doc that the class "fields" is very important, and how to use it well.