1

我使用了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 < ActiveRecord::Base belongs_to :supplier 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
4

3 回答 3

2

你必须添加

attr_accessible :items_attributes

这是文档的链接:)

于 2012-08-01T08:41:21.477 回答
0

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

您的 GoodsIn 需要具有 accept_nested_attributes_for :items 的关系。belongs_to 和accepts_nested_attributes_for 是关闭的。

于 2012-10-09T05:22:20.083 回答
0

I think there's an error in your Goods model.

It should read has_many :items instead of has_many :item.

于 2012-08-01T08:30:07.337 回答