I\'m having a difficult time understanding how to do this. I have two models, a project, and a course.
#project.rb
belongs_to :course
attr_accessible :course_id,
You should be using the form builder to build out your object, rather than just fields_for
by itself.
You have this:
= fields_for :course do |builder|
Where you should have this:
= f.fields_for :course do |builder|
The difference is that by calling it on the initial form builder, Rails will check to see if there is a course_attributes=
method on the object from the initial form_for
call (in this case, that'd be @project
) and if there is then it'll define the fields inside this form as being course_attributes
.
Go ahead and inspect the form before and after this change, just to see. I'll wait.
This is made possible by the accepts_nested_attributes_for
call in your model. It's this method that defines the course_attributes=
method that allows for the nested attributes to work. Once you create the project, it should then also create the course.
Also, no need to make course_id
an accessible attribute, as your form's not going to be setting that.