0

我将nested_form 用于以下情况:

Parent (climb) ==has_one==> Join Model (route_ascent) ==多态 has_many==> Children (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

在我的 Climb 表格中,我有

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 关联是否以对象开头,都会出现问题。所以如果它是空的,我可以成功创建一个对象,但不能创建两个。如果它已经有一个对象,我不能再添加一个对象而不会出现此错误。

将继续努力解决这个问题,但我会很感激任何关于问题可能是什么的见解!

4

1 回答 1

0

乍一看,似乎第二个 ascent_steps_attributes 具有相同的“标识符”“0”,这将与第一个冲突。如果您还没有在 irb 中测试过这个数据,那将是一个很好的地方来看看您是否可以使用这个输入创建您的数据对象。

于 2011-06-06T20:34:10.327 回答