【问题标题】:unknown attribute error when submitting multiple records in double-nested form以双嵌套形式提交多条记录时出现未知属性错误
【发布时间】:2011-06-07 04:34:10
【问题描述】:

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

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

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

【问题讨论】:

    标签: ruby-on-rails nested-forms nested-form-for


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多