How to make Rails controller accept attributes
chrislawrence opened this issue · 6 comments
I have set up the form and is working perfectly on front end but I can't make the rails controller accept nested attributes for new objects. I can edit existing children but the "new_tasks" parameter is being rejected by strong parameters.
In the controller, you must have st like that:
params.require(:project).permit(:title, :public, tasks_attributes: [:id, :title, :_destroy])
In project
model, you need to add:
accepts_nested_attributes_for :tasks, allow_destroy: true
This works for existing objects but the controller rejects new_tasks
within the tasks_attributes
hash.
@chrislawrence Can you show me the parameters log? For example:
{"authenticity_token"=>"[FILTERED]", "project"=>{"name"=>"New year", "tasks_attributes"=>{"0"=>{"title"=>"Practice Violin", "completed"=>"0", "_destroy"=>"false", "id"=>"14"}, "1"=>{"title"=>"Learn chinese", "completed"=>"0", "_destroy"=>"false", "id"=>"15"}, "1707794668732"=>{"title"=>"Test", "completed"=>"0", "_destroy"=>"false"}}}, "commit"=>"Create project", "id"=>"3"}
I am using a patient
model with has_many diagnoses
. Here is the parameters hash:
Parameters: {"authenticity_token"=>"[FILTERED]", "patient"=>{"mrn"=>"1234567", "active"=>"1", "first_name"=>"Test", "last_name"=>"Patient", "diagnoses_attributes"=>{"0"=>{"name"=>"test", "icd_id"=>"56", "_destroy"=>"false", "id"=>"2"}, "new_diagnoses"=>{"name"=>"test2", "icd_id"=>"2", "_destroy"=>"false"}}}, "commit"=>"Update Patient", "id"=>"2"}
@chrislawrence I fixed this problem, I'll release a new version soon.
Before you can fetch this gem from GitHub source
gem 'rondo_form', git: 'https://github.com/hungle00/rondo_form.git', branch: 'master'
Upgrade to version 0.2.5 will fix this error.