简体   繁体   中英

Mass Assignment error in nested form

i'm having issues with my nested form when submiting it.

My two models:

#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

and in my form:

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

Everything is showing just like I wanted until I submit, when it shows the following:

Can't mass-assign protected attributes: period

Does anyone knows how to help me? Been searching the whole web...

Thanks!

you don't need the @planning.periods.each, just do this

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

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

attr_accessible :planned_quantity, :planning_id, :period

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM