Strong parameters with has_and_belongs_to_many
Given you have a product that can take many category ids (checkboxes), you need to have that key and value type in the controller:
def product_params
params.require(:product).permit(:name, :description, :category_ids => [])
end
Written by Cezar Halmagean
Related protips
6 Responses
Thanks for this, I was getting mixed up about whether I had to do this with nestedattributesfor in Rails 4...
over 1 year ago
·
you're welcome :)
over 1 year ago
·
You saved my day :)
over 1 year ago
·
@marcopotento awesome :)
over 1 year ago
·
thanks so much! I had no idea why the list of ids from the habtm relationship wouldn't save correctly, this fixed it!
over 1 year ago
·
glad to know it worked
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#