Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
852 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...