unknown attribute error when submitting multiple records in double-nested form
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">开发者_如何学JAVA;
<%= 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!
From a quick glance it seems like the second ascent_steps_attributes has the same "identifier" "0" which would conflict with the first one. If you haven't already tested this data in irb that will be a good place to see if you can create your data objects with this input.
精彩评论