Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Aidid Jaafar
PLUS
Aidid Jaafar
Courses Plus Student 4,354 Points

Rails Nested Attributes Form

Hi Everyone, I am seeking for help as I am stuck with Rails accets_nested_attributes_for, I had Mass Assignment Problem when I try to submit a form.

Here is my model:

class Project < ActiveRecord::Base
# Relationship 
#belongs_to :role
has_many :roles
belongs_to :user

#has_many :users, :through => :roles

accepts_nested_attributes_for :roles
attr_accessible :project_name, :sprint_duration, :roles_attributes

end

class Role < ActiveRecord::Base
attr_accessible :project_id, :user_id, :user_role

belongs_to :project
end

My Form which are in partial _form.html.erb

<%= form_for Project.new do |f| %>
<%= f.label :project_name %>
<%= f.text_field :project_name %>
<%= f.fields_for :roles do |builder| %>
<%= builder.label :user_role %>
<%= builder.text_field :user_role %>
<% end %>
<%= f.submit %>
<% end %>

I am half retard thinking about this, hope to get some pointer on this, thank you in advance !!!

5 Answers

Wayne Fonseca
Wayne Fonseca
7,796 Points

What is the error message?

Aidid Jaafar
PLUS
Aidid Jaafar
Courses Plus Student 4,354 Points

I had this error: Can't mass-assign protected attributes: role

Wayne Fonseca
Wayne Fonseca
7,796 Points

ruby attr_accessible :project_name, :sprint_duration, :roles_attributes

Please confirm if the :roles_attributes is correct spelling?

Aidid Jaafar
PLUS
Aidid Jaafar
Courses Plus Student 4,354 Points

Thank you Wayne Fonseca, but I think :roles_attributes is correct. I am not sure what is the main caused of this nested model errors. Thank you for your help, I will keep looking.

Wayne Fonseca
Wayne Fonseca
7,796 Points

Hi Jaafar,

Could you send me the link on the video you are working on?