嵌套表单中的批量分配错误

时间:2013-10-30 14:10:36

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

我在提交时遇到嵌套表单的问题。

我的两个模特:

#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

有谁知道如何帮助我?一直在搜索整个网络......

谢谢!

2 个答案:

答案 0 :(得分:0)

你不需要@ planning.periods.each,只需这样做

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

答案 1 :(得分:0)

attr_accessible中定义您的属性,如下所示:

attr_accessible :planned_quantity, :planning_id, :period