Project and tasks have a one-to-many relationship, and project accepts_nested_attributes_for :tasks.
In the form, my task objects look like:
project[tasks][2][assigned_time]
project[tasks][2][due_time]
When the form is submitted I get a hash like:
Parameters: {“utf8″=>”✓”, “authenticity_token”=>”…=”,
“project”=>{“id”=>”1”, “tasks”=>{“1″=>{“assigned_time”=>”09:00”,
“due_time”=>”17:00”}, “2”=>{“assigned_time”=>”09:00”,
“due_time”=>”17:00”}}
Then I expect them to be saved by just saving the project object:
project = Project.find(params[:id])
respond_to do |format|
if project.update_attributes(params[:tasks])
But I get:
WARNING: Can’t mass-assign protected attributes: id SQL (0.3ms)
ROLLBACK Completed in 169msActiveRecord::AssociationTypeMismatch (Task(#2188181260) expected, got
Array(#2151973780)):
Any ideas how to fix this?
In your
Projectsmodel,accepts_nested_attributes_for :tasks. This will define@project.tasks_attributes=if you have ahas_many :tasksassociation or@project.task_attributes=if you have ahas_one :taskassociation.In your form, the following:
In your projects controller, the following: