Nested Forms and Strong Attributes
I’m working on a Rails 4 project with that has a model with a self-referential has_many :through => setup. Using Simple Form and Nested Form made short work out of creating new nested models, but for some reason the update action wasn’t working correctly.
Here’s my original code to permit parameters, under the new strong parameters model. Can you spot the bug?
params.require(:website).permit(:name, :url, :owner_name, | |
:competing_websites_attributes => [:name, :url, :owner_name]) |
I wasn’t allowing the nested model’s id through, so it was trying to create a new website every time I updated the parent website.
params.require(:website).permit(:name, :url, :owner_name, | |
:competing_websites_attributes => [:name, :url, :owner_name, :id]) |
Adding :id to the nested allowed attributes fixed the issue.