nested_form icon indicating copy to clipboard operation
nested_form copied to clipboard

double nested_forms

Open tod-uma opened this issue 11 years ago • 4 comments

I am having an issue trying to use double nested forms. Basically I'm trying to make a form that has objects nested inside other nested objects. Everything is working fine except when you attempt to add a new object to the second single nested object it will add it to the first new nested object instead. For example, given this example form:

<%= nested_form_for @exam do |f| %> <%= content_for :div, :class => 'field' do %> <%= f.label :title %> <%= f.text_field :title %> <% end %>

<%= f.fields_for :questions do |question_form| %> <%= question_form.label :title %> <%= question_form.text_field :title %> <%= question_form.link_to_remove "[-] Remove" %> <%= question_form.fields_for do |answer_form| %> <%= answer_form.text_field %> <%= answer_form.link_to_remove "[-] Remove" %> <% end %> <% end %> <% end %>

and the classes:

class Exam < ActiveRecord::Base has_many :questions end class Question < ActiveRecord::Base belongs_to :exam has_many :answers end class Answer < ActiveRecord::Base belongs_to :question end

The form mostly works fine when you only add one question before you submit. However, if you add a second question before submitting, from now on all answers added will be added to the first question. Also, the remove links seem remove everything, not just the object you're removing.

(the form above is greatly reduced from my actual code. I just wanted everyone to understand what I'm trying to accomplish. I may have introduced more errors, etc from what I'm seeing)

tod-uma avatar Apr 09 '13 19:04 tod-uma

Can you please provide a sample application that reproduces the error?

lest avatar Apr 10 '13 10:04 lest

Update: nvm, I thought I was on the latest master, but wasn't

garrettlancaster avatar Apr 25 '13 15:04 garrettlancaster

I have the same problem. In my case it's more tricky because I use tables nested in a table. My nesting scheme is like this : Etude has_many :phases Phase has_many :affectations

Note : I'm using f.index to properly tag the nested tables with the good ID: *:data => { :target => "##{f.index}" } ...

_phase_fields.html.erb

    <tr>
        <td rowspan="3">
            <stuff />
            <%= f.link_to_remove '<i class="glyphicon glyphicon-trash"></i>'.html_safe, class: "btn btn-danger", data: {association: 'phase'} %>
            <stuff />
        </td>
        <many-td-stuff />
    </tr>
    <tr>
        <td colspan="6">
        <table id="<%= f.index %>" class="table table-striped table-bordered table-hover liste-intervenants">
            <thead>
                <stuff />
            </thead>
            <tbody><%= f.fields_for :affectations, :wrapper => false %></tbody>
        </table>
        </td>
    </tr>
    <tr>
        <td colspan="6">
            <% addi = ("<i class='glyphicon glyphicon-user'></i> <i class='glyphicon glyphicon-plus'></i> " + t("add_intervenant")).html_safe %>
            <%= f.link_to_add addi, :affectations, :data => { :target => "##{f.index}"}, class: "btn btn-sm btn-success" %>
        </td>
    </tr>

On a sidenote, I don't really understand if I should use the "fields" class anywhere as suggested by https://github.com/ryanb/nested_form/wiki/How-To:-Render-nested-fields-inside-a-table. Is it actually used by the gem or not ? If yes, how exactly ?

Startouf avatar Nov 09 '14 21:11 Startouf

Okay. So if you have a look at the main .js of the gem, you'll see that everything is based on a particular class "fields" that you MUST add if you want to gem to work properly.

In a nutshell, you should encapsulate your fields by a tag with the class "fields" For example, if like me you have a partial that consists of many

, well you'll have to wrap them in a

However, this should only be a problem if you don't use the default

generation (without specifying :wrapper => false and :data => { :target =>)

This is not actually a bug, but it's not explained very well in the doc either...

Startouf avatar Nov 10 '14 23:11 Startouf