Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

unknown attribute error when submitting multiple records in double-nested form #51

Closed
cmezak opened this Issue Jun 6, 2011 · 6 comments

Comments

Projects
None yet
3 participants

cmezak commented Jun 6, 2011

I'm using nested_form for a situation where:

Parent (climb) ==has_one==> Join Model (route_ascent) ==polymorphic has_many==> Children (route_step)

So I have a climb object that looks like

class Climb < ActiveRecord::Base
  has_one :route_ascent
  accepts_nested_attributes_for :route_ascent
end

Here's RouteAscent

class RouteAscent < ActiveRecord::Base
  has_many :ascent_steps, :class_name => 'RouteStep', :as => :steppable
  accepts_nested_attributes_for :ascent_steps, :allow_destroy => true
end

And Here's RouteStep

class RouteStep < ActiveRecord::Base
  belongs_to :steppable, :polymorphic => true
end

In my Climb form I have

f.fields_for :route_ascent

My _route_ascent_fields partial is simply

<%= f.fields_for :ascent_steps %>
<p><%= f.link_to_add "Add A Step", :ascent_steps %></p>

And my _ascent_step_fields partial is

<div class="field">
<%= f.label :order %>
<%= f.text_field :position %><br>
<%= f.label :description %>
<%= f.text_area :description %>
<%= f.link_to_remove "Remove Step" %>
</div>

The problem I have is that whenever I submit the form with more than one object in the join model's has_many association, I get an unknown attribute error. Here's what the parameters look like that are generated by the form in such a case:

"route_ascent_attributes"=>{"ascent_steps_attributes"=>{"0"=>{"position"=>"1",
 "description"=>"this will also work",
 "_destroy"=>"false",
 "id"=>"66"}},
 "0"=>{"new_1307386880995"=>{"position"=>"2",
 "description"=>"broken!",
 "_destroy"=>"false"}},
 "id"=>"4"},

It looks like the second object is not being included correctly in the parameters, but I haven't been able to figure out why this is so.

The problem occurs whether or not the has_many association starts out with an object or not. So If it is empty, I can successfully create a single object but not two. If It already has one object, I can't add a second without getting this error.

Will continue to work on this, but I'd appreciate any insight as to what the problem might be!

cmezak commented Jun 6, 2011

Commenting out lines 15-31 in nested_form.js bypasses the problem, if that's any help.

Those lines are:

    // context will be something like this for a brand new form:
    // project[tasks_attributes][new_1255929127459][assignments_attributes][new_1255929128105]
    // or for an edit form:
    // project[tasks_attributes][0][assignments_attributes][1]
    if(context) {
      var parent_names = context.match(/[a-z_]+_attributes/g) || [];
      var parent_ids   = context.match(/(new_)?[0-9]+/g) || [];

      for(i = 0; i < parent_names.length; i++) {
        if(parent_ids[i]) {
          content = content.replace(
            new RegExp('(_' + parent_names[i] + ')_.+?_', 'g'),
           '$1_' + parent_ids[i] + '_');

          content = content.replace(
            new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
            '$1[' + parent_ids[i] + ']');
        }
      }

    }
Owner

ryanb commented Jun 8, 2011

Thanks for reporting this issue. I don't have time at the moment to research this, but it will be very helpful once I get a chance.

cmezak commented Jun 9, 2011

Thanks Ryan. No rush at all – you do so much for the community as it is. But let me know if I can provide any additional info or testing.

Owner

ryanb commented Jun 29, 2011

Just to give you an update, the plan is to get a good test suite in place next, see issue #22 for this. Then we can duplicate problems like this and find a concrete solution and ensure nothing else breaks. If you have input on testing please comment there.

Collaborator

lest commented Sep 19, 2012

@cmezak Could you please test with the latest nested_form version? I'd be helpful if you provided a test application reproducing this issue. Thanks!

Collaborator

lest commented Oct 18, 2012

Closing for now. Will reopen if there is still an issue.

@lest lest closed this Oct 18, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment