Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
cmezak opened this Issue · 6 comments

3 participants

Charlie Mezak Ryan Bates Sergey Nartimov
Charlie Mezak

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!

Charlie Mezak

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] + ']');
        }
      }

    }
Ryan Bates
Owner

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.

Charlie Mezak

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.

Ryan Bates
Owner

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.

Sergey Nartimov
Collaborator

@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!

Sergey Nartimov
Collaborator

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

Sergey Nartimov lest closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.