Nested strong parameters in rails - AssociationTypeMismatch MYMODEL expected, got ActionController::Parameters()
Are you using accepts_nested_attributes_for :books
on your project model? If so, instead of "books"
, the key should be "books_attributes"
.
def project_params
params.permit(:id, :complete, :false, :private, books_attributes: [:id, :name])
end
I'm using Angular.js & Rails & Rails serializer, and this worked for me:
Model:
- has_many :features
- accepts_nested_attributes_for :features
ModelSerializer:
- has_many :features, root: :features_attributes
Controller:
- params.permit features_attributes: [:id, :enabled]
AngularJS:
- ng-repeat="feature in model.features_attributes track by feature.id
My solution to this using ember.js was setting the books_attributes mannualy.
In controller:
def project_params
params[:project][:books_attributes] = params[:project][:books_or_whatever_name_relationships_have] if params[:project][:books_or_whatever_name_relationships_have]
params.require(:project).permit(:attr1, :attr2,...., books_attributes: [:book_attr1, :book_attr2, ....])
end
So rails checks and filters the nested attributes as it expected them to come