Reputation:
I really can't figure this one out. I've read a lot of similar problems regarding the mass assignment error but every solution I've tried has failed.
I'm trying to nest my institution model into my course model using accepts_nested_attributes_for & simple_form. My code is as follows:
Course Model:
# Attributes
attr_accessible :class_end, :class_start, :cost, :effort, :level, :name, :overview, :prerequisites, :tags, :tag_list
# Associations
belongs_to :institution
accepts_nested_attributes_for :institution
Institution Model:
# Attributes
attr_accessible :bio, :city, :country, :name, :state, :twitter_url, :type, :url, :image_url, :email
# Associations
has_many :courses
View of nested simple_form:
<%= simple_form_for(@course) do |f| %>
<%= f.simple_fields_for :institutions do |i| %>
<%= i.input :name %>
<%= f.input :name %>
<%= f.input :overview %>
<%= f.input :cost %>
<%= f.input :level %>
<%= f.input :tag_list %>
<%= f.input :class_start %>
<%= f.input :class_end %>
<%= f.input :effort %>
<%= f.input :prerequisites %>
<%= f.button :submit %>
<% end %>
<% end %>
The error I'm getting:
Can't mass-assign protected attributes: institutions
{"utf8"=>"✓",
"authenticity_token"=>"GRoBHYhpv3QyzvH2UHBaJQ/62+9QIDKIwp/VLiLMjus=",
"course"=>{"institutions"=>{"name"=>"asdfa"},
"name"=>"sdaf",
"overview"=>"asdf",
"cost"=>"Free",
"level"=>"Beginner",
"tag_list"=>"asdf",
"class_start(1i)"=>"2012",
"class_start(2i)"=>"9",
"class_start(3i)"=>"18",
"class_end(1i)"=>"2012",
"class_end(2i)"=>"9",
"class_end(3i)"=>"18",
"effort"=>"asdf",
"prerequisites"=>"asdf"},
"commit"=>"Create Course"}
Upvotes: 1
Views: 848
Reputation: 1
Have you 'built' the institution in your controller?
try including:
@course.build_institution
In your CoursesController
Upvotes: 0
Reputation: 2929
You need to add institution_attributes to attr_accessible of the Course and use proper name for fields_for, I think it should be :institution, not :institutions. Form should look like this:
<%= simple_form_for(@course) do |f| %>
<%= f.simple_fields_for :institution do |i| %>
<%= i.input :name %>
<% end %>
<%= f.input :name %>
<%= f.input :overview %>
<%= f.input :cost %>
<%= f.input :level %>
<%= f.input :tag_list %>
<%= f.input :class_start %>
<%= f.input :class_end %>
<%= f.input :effort %>
<%= f.input :prerequisites %>
<%= f.button :submit %>
<% end %>
Upvotes: 2