2013-10-30 29 views
0

我在提交它時遇到了嵌套形式的問題。嵌套形式的質量賦值錯誤

我的兩個型號:

#PLANNING MODEL 
class Planning < ActiveRecord::Base 
    has_many :periods 
    belongs_to :plannable, polymorphic: true 
    attr_accessible :quantity, :periods_attributes 
    accepts_nested_attributes_for :periods 
end 

#PERIOD MODEL 
class Period < ActiveRecord::Base 
    belongs_to :planning 
    attr_accessible :planned_quantity, :planning_id 
end 

,並在我的形式:

... 
<% @planning.periods.each do |period| %> 
    <%= f.fields_for(period) do |builder| %> 
    <%= builder.label :planned_quantity, "Planned quantity" %> 
    <%= builder.number_field :planned_quantity%> 
    <%end%> 
<%end%> 
... 

一切都顯示,就像我想直到我提出,當它顯示以下內容:

Can't mass-assign protected attributes: period 

有誰知道如何幫助我?正在搜索整個網絡...

謝謝!

+0

您是否嘗試過加入':period'到規劃模型中的attr_accessible白名單? – rjz

+0

@rjz是的,已經試過了,它返回錯誤'unknown attribute:period' – ianobraczka

回答

0

你不需要@ planning.periods.each,只是這樣做

<%= f.fields_for :periods do |builder| %> 
    <%= builder.label :planned_quantity, "Planned quantity" %> 
    <%= builder.number_field :planned_quantity%> 
<% end %> 
+0

試過了,現在它在我的表單中沒有顯示任何記錄,就好像我沒有創建任何時間段(我是'3 times {planning.periods.build}''在plannings_controller/new' – ianobraczka

0

attr_accessible定義屬性,因爲:

attr_accessible :planned_quantity, :planning_id, :period 
+0

這是否有效嗎? –

+0

不,我收到消息'unknown attribute:period'。爲什麼我需要a:planning_id屬性在規劃模型中?! – ianobraczka