Rails嵌套表单 - 无法批量分配受保护的属性:

时间:2012-08-01 08:17:42

标签: ruby-on-rails nested-forms mass-assignment

我已经使用了nested_form gem,每当我尝试向表单提交内容时,我都会收到Can't mass-assign protected attributes:items消息,即使我已经将attr_accessible放入我的模型中。

形式:

<%= nested_form_for(@goods_in) do |f| %>
...

<%= f.fields_for :items do |i| %>
<td><%= i.text_field :description, :autocomplete => :off%></td>
<td><%= i.text_field :quantity, :autocomplete => :off %></td>
<th><%= i.link_to_remove "Remove this item" %></th>
<% end %>
<%= f.submit :"Submit Delivery" %>
<% end %>

模型中的货物:类GoodsIn&lt;的ActiveRecord :: Base的   belongs_to:供应商   has_many:items

attr_accessible :c4lpono, 
              :courier, 
              :deliverydate,  
              :deliverynoteno,  
              :destination,  
              :notes,  
              :quantity,  
              :signedby,
              :supplier_id,
              :partcode_ids

accepts_nested_attributes_for :supplier

validates :c4lpono, 
              :deliverydate,  
              :deliverynoteno,  
              :destination,  
              :quantity,  
              :signedby,
              :presence =>true                  

end

项目模型

class Item < ActiveRecord::Base
belongs_to :goods_in

attr_accessible :quantity,
              :partcode,
              :description,
              :goods_in_id


accepts_nested_attributes_for :goods_in


end

控制器中的货物:

def create
@goods_in = GoodsIn.new(params[:goods_in])
end

3 个答案:

答案 0 :(得分:2)

你必须添加

attr_accessible :items_attributes

这里是a link文档:)

答案 1 :(得分:0)

我认为您的商品模型中存在错误。

它应该是has_many :items而不是has_many :item

答案 2 :(得分:0)

很难说你想要用你的模型实现什么,但我认为你想要以下内容:

您的GoodsIn需要拥有accepts_nested_attributes_for:items的关系。 belongs_to和accepts_nested_attributes_for已关闭。