nested form triggering a 'Can't mass-assign protected attributes warning

为君一笑 提交于 2019-11-28 02:42:11

问题


I've got a multi layer nested form

User->Tasks->Prerequisites 

and in the same form

User->Tasks->Location

The location form works fine, now I'm trying to specify prerequisites to the current task. The prerequisite is a task_id stored in the :completed_task field.

When I submit the form, I get the following error in the output

WARNING: Can't mass-assign protected attributes: prerequisite_attributes

One warning for each task in the user.

I've gone through all the other questions related to this, ensuring that the field name :completed_task is being referenced correctly,

adding attr_accessible to my model (it was already there and I extended it).

I'm not sure what else i'm supposed to be doing.

My models look like

class Task < ActiveRecord::Base
     attr_accessible :user_id, :date, :description, :location_id

     belongs_to :user
     has_one :location
     accepts_nested_attributes_for :location 
     has_many :prerequisites
     accepts_nested_attributes_for :prerequisites
end

class Prerequisite < ActiveRecord::Base
     attr_accessible :completed_task

     belongs_to :task
end

the form uses formtastic, and I'm including the form via

<%= f.semantic_fields_for :prerequisites do |builder3| %>
    <%= render 'prerequisite_fields', :f=>builder3 %>
<% end %>

--- _prerequisite_fields.html.erb -----
< div class="nested-fields" >
   <%= f. inputs:completed_step %>
</div>

Any suggestions?


回答1:


Add :prerequisite_attributes to attr_accessible in order to mass-assign

attr_accessible :user_id, :date, :description, :location_id, :prerequisite_attributes

Should get you started.



来源:https://stackoverflow.com/questions/5037239/nested-form-triggering-a-cant-mass-assign-protected-attributes-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!