1

I'm using Rails 4 and I have 3 models: property, category, and categorization.

The application is here: https://github.com/ornerymoose/PropertiesAndCategories

The idea: you can update a property and there's 6 categories (6 checkboxes) to choose from.

I came across this SO post and it was helpful but alas, my values still aren't getting saved.

When I click the update button to update a property: the server logs (if I select checkboxes 3 4 and 5) will be:

**Processing by PropertiesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Dcgy+3qBF4JLeu5MMATyqOc+jWyILXTfgURbqn+Ez8E2ru3rpzgPBqKDPWsy/QelQuQgOczdC8xDsOnnwSDAnQ==", "property"=>{"name"=>"Vizcaya", "category_id"=>["3", "4", "5", ""]}, "commit"=>"Update Property", "id"=>"6"}**

If I fire up rails console and check out the ^above property, category_id is set to nil. Why is this? My strong params are correct in the properties_controller I think:

def property_params
  params.require(:property).permit(:name, :category_id => [])
end

Any and all input is greatly appreciated.

Community
  • 1
  • 1
Nubtacular
  • 1,367
  • 2
  • 18
  • 38

2 Answers2

1

Why do you have ":category_id => []"? I'm pretty sure if you get rid of the "=> []" portion and just treat :category_id like :name (as I've shown below), it should work!

def property_params
  params.require(:property).permit(:name, :category_id)
end
JuliaC
  • 121
  • 1
  • 2
  • 11
1

Ok, the first fix was here in the properties controller:

def property_params
  params.require(:property).permit(:name, :category_ids => [])
end

I was initially using :category_id => [] which didn't return an error in the Rails server log, but wasn't saving the checkbox values to the database.

And then the second fix was fixing the collection_check_boxes arguments. Initially I had :category_id but I completely overlooked that it had to be :category_ids:

<%= collection_check_boxes(:property, :category_ids, Category.all, :id, :name)

Everything is working well now.

Nubtacular
  • 1,367
  • 2
  • 18
  • 38