2012-08-01 73 views
1

我已經使用nested_form gem,只要我嘗試並向我的表單提交某些東西,即使我已將attr_accessible放入我的模型中,我也會收到Can't mass-assign protected attributes:items消息。Rails嵌套表單 - 無法批量分配受保護的屬性:

形式:

<%= 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的:供應商 的has_many:項目

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 
+0

你可以在哪裏調用新的,創建或update_attributes顯示相關的控制器代碼? – 2012-08-01 08:25:19

+0

添加到我的問題! – 2012-08-01 08:27:14

回答

0

我認爲您的貨物模型中有錯誤。

它應該讀取has_many :items而不是has_many :item

+0

我改變了這一點,得到這個:'沒有名稱項目的關聯'。它已被定義了嗎?' – 2012-08-01 08:31:37

+0

如果您更改了'accep_nested_attributes_for:items'而不是':item',該怎麼辦? – 2012-08-01 08:33:14

+0

如果我改變它們都項目而不是項目,頁面加載但字段不顯示,如果我只是改變接受嵌套屬性部分我得到這個:'沒有名稱項目發現關聯'。它是否已被定義?' – 2012-08-01 08:35:23

2

你必須添加

attr_accessible :items_attributes 

而這裏的a link到文檔:)

+0

我以前試過,它不會改變任何東西:( – 2012-08-01 08:43:25

+0

你能告訴我正在發送的日誌PARAMS? – pkurek 2012-08-01 09:13:56

+0

{「utf8」=>「✓」, 「 authenticity_token 「=>」 GBF/8dANkgi9fCWfwluL3drfpksHMReBK1zJveswulE =」, 「goods_in」=> { 「deliverydate」=> 「」, 「deliverynoteno」=> 「」, 「目的地」=> 「」, 「c4lpono」=> 「」, 「courier」=>「」, 「notes」=>「」, 「signedby」=>「Carla Dessi」, 「items」=> {「partcode」=>「」, 「description 「=>」「, 」quantity「=>」「, 」_destroy「=>」「}}, 」suppliers「=> {」suppliername「=>」「}, 「commit」=>「Submit Delivery」, 「collection」=> {「view」=>:get}} – 2012-08-01 09:15:25

0

很難告訴你正在努力實現用你的模型是什麼,但我想你想的folllowing:

您的GoodsIn需要與accep_nested_attributes_for:items有關係。 belongs_to和accep_nested_attributes_for已關閉。

相關問題