Last Updated: February 05, 2018
·
6.354K
· mixandgo

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

6 Responses
Add your response

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 ·