0

次のような状況でnested_formを使用しています。

親 (climb) ==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"},

2 番目のオブジェクトがパラメーターに正しく含まれていないようですが、その理由がわかりません。

この問題は、has_many 関連付けがオブジェクトで始まるかどうかに関係なく発生します。したがって、空の場合、1 つのオブジェクトを正常に作成できますが、2 つ作成することはできません。既に 1 つのオブジェクトがある場合、このエラーが発生しないと 2 番目のオブジェクトを追加できません。

これに引き続き取り組みますが、問題が何であるかについての洞察をいただければ幸いです。

4

1 に答える 1

0

一見すると、2 番目の ascent_steps_attributes には、最初のものと競合する同じ「識別子」「0」があるように見えます。このデータを irb でまだテストしていない場合は、この入力を使用してデータ オブジェクトを作成できるかどうかを確認するのに適しています。

于 2011-06-06T20:34:10.327 に答える