ruby on rails-以双嵌套形式提交多个记录时出现未知属性错误



我使用nested_form来处理以下情况:

父级(爬升)===has_one===加入模型(route_escape)===多态has_many===子级(route_step)

所以我有一个看起来像的攀爬物体

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

这是RouteAscent

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

这是RouteStep

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

在我的攀登表格中,我有

f.fields_for :route_ascent

我的_route_scent_fields部分只是

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

我的_场景_步骤_字段部分是

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

我遇到的问题是,每当我提交带有联接模型的has_many关联中的多个对象的表单时,我都会得到一个未知的属性错误。以下是在这种情况下表单生成的参数:

"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"},

看起来第二个对象没有被正确地包含在参数中,但我还没能弄清楚为什么会这样

无论has_many关联是否从对象开始,都会出现问题。所以如果它是空的,我可以成功地创建一个对象,但不能创建两个。如果它已经有一个对象,我不能再添加一个对象而不出现这个错误。

我会继续做这方面的工作,但我很乐意了解问题可能是什么!

快速浏览,第二个ascent_steps_attributes似乎具有相同的"标识符"0",这将与第一个发生冲突。如果你还没有在irb中测试过这些数据,那么这将是一个很好的地方,看看你是否可以用这个输入创建你的数据对象。

最新更新