问题描述
|
我在以下情况下使用nested_form:
父级(攀爬)== has_one ==>联接模型(route_ascent)==多态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_ascent_fields部分很简单
<%= f.fields_for :ascent_steps %>
<p><%= f.link_to_add \"Add A Step\",:ascent_steps %></p>
我的_ascent_step_fields部分为
<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具有相同的\“ identifier \” \“ 0 \”与第一个相同。如果您尚未在irb中测试此数据,那将是查看是否可以使用此输入创建数据对象的好地方。