Rails Nested Form - elements added dynamically all get index zero

I am having problems with my nested form. I am using Ryan Bates code. Gemfile:

gem "nested_form" ,: git => 'https://github.com/ryanb/nested_form.git'

The problem is that when I try to use * link_to_add * I get a new item that looks fine, but it doesn't work because it gets a new index of 0 instead of a "random" index based on the current time that the nested_form file is supposed to be created. js. Every element I add gets the same index 0. So the effect is that I can't add a new element (or only the last new one is saved depending on the original state of the model).

The behavior is the same with the new parent and when editing the parent.

I am sure I am doing something wrong as I was able to make a simple new application that works. But I can't figure out what is wrong with my real application.

The nested_form.js generated for my (production) test application and my (non-working) real application is identical. So I don't think there is a version difference issue.

I used Firebug to execute the code in the nested_form.js file but couldn't figure out why it was generating index 0 there.

My parent model has this:

class Course <ActiveRecord :: Base

accepts_nested_attributes_for :levels, :allow_destroy => true
has_many :levels, :dependent => :destroy, :order => :depth

      

My form is created this way:

<%= nested_form_for(@course) do |f| %>

      

The nested object part looks like this:

<%= f.fields_for :levels do |builder| %>
    <tr>
        <td><%= builder.text_field :name %></td>
        <td><%= image_tag(builder.object.icon) unless (builder.object.icon.nil?) %></td>
        <td><%= builder.link_to_remove "Remove" %></td>
    </tr>
<% end %>
   <tr><td><%= f.link_to_add "Add a level", :levels %></td></tr>

      

I don't see any difference between my code and my working example (or other examples I've seen).

Does anyone have any ideas?

UPDATE:

After trying to debug this back in Firebug, I realized that the "project" used by the addFields function in nested_form.js was modified inside the addFields method and why it was not putting the generated index (this block was bypassing my test application). So I tried the block where this happens by changing:

if (context) {

      

in

if (false)

      

This seems to fix my symptom from pre-testing, but I feel like in some contexts (NPI) it might break something else (maybe with multiple levels of nesting?).

+3


source to share


3 answers


While I feel a little silly, I think it might happen to someone else (and I did say in my question that I was probably doing something "wrong").



What I was doing "wrong", I was using margins to separate areas of my form. And I was giving the fields a class field. Not good. Conflicts with the form generator classes.

+2


source


I ran into a similar situation where my parent did not have its own fields:

Carousel has_many Items
<%= f.inputs 'Carousel' do %>
<!-- NOTICE NO FIELDS HERE -->

  <f.fields_for :items, allow_destroy: true do |it| %>
    <%= it.input :media_hash %>
    <%= it.link_to_remove 'Remove item' %>
  <% end %>
  <%= f.link_to_add 'Add item', :items %>
<% end %>

      



By adding the field to the parent, the indexes were created correctly. In my case, I just added f.input :created_at, as: :hidden

to my carousel.

+1


source


I have the same problem (over updates, not creation), except that only instances of the "fields" classes in my html are generated by the fields_for app, so very frustrated.

Spent almost a full day stepping over javascript but see no reason why it should replace the strings "new_" with invalid indices "0".

My hack that fixed this was adding a check in the content edit loop:

for(var i = 0; i < parentNames.length; i++) {
          if (parentIds[i] && parentIds[i] !== "0") {
            content = content.replace(
              new RegExp('(_' + parentNames[i] + ')_.+?_', 'g'),
              '$1_' + parentIds[i] + '_');
           ...

      

0


source







All Articles