How do I loop through params in my controller so that I can change their values?
This:
...
params[:contact][:relationships_attributes].each do |rel|
rel[:contact_ids] = [rel[:contact_ids]]
end
...
Returns an Error of:
# can't convert Symbol into Integer
To provide more context I am using a single select box in place of a multi-select. So the controller is expecting an array but instead its getting a single value.
Here Are My Params:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"8FrW59t9vzSep+jEGceMdE/b5q69i5hrEp08yZJJk0o=",
"contact"=>
{"first_name"=>"John",
"last_name"=>"Smith",
"email_addresses_attributes"=>
{"0"=>
{"value"=>"john.smith@gmail.com",
"type"=>"Home",
"_destroy"=>"false",
"id"=>"4f4eea6dbe5a2ccf03000031"
},
"1"=>{"value"=>"john@smith.com",
"type"=>"Home",
"_destroy"=>"false",
"id"=>"4f4eea7dbe5a2ccf03000035"
}
},
"phone_numbers_attributes"=>
{"0"=>
{"value"=>"555-334-5959",
"type"=>"Home",
"_destroy"=>"false",
"id"=>"4f4eea64be5a2ccf0300002c"}
},
"addresses_attributes"=>
{"0"=>
{"type"=>"Home",
"street_address"=>"12345 Any Rd",
"city"=>"Anytown",
"state"=>"CO",
"country"=>"United States",
"_destroy"=>"false",
"id"=>"4f4eea64be5a2ccf0300002d"
}
},
"relationships_attributes"=>
{"1330623965418"=>
{"contact_ids"=>"4f4e6696be5a2ca2a7000045",
"title"=>"Wife",
"_destroy"=>"false"
}
}
},
"commit"=>"Update Contact",
"id"=>"4f4e6696be5a2ca2a7000045"
}
When you use
eachon a Hash like this:the block argument,
rel, is actually a two element Array with the key at position 0 and the value at position 1. Then your block tries to access the Array as though it was a Hash:and that explains your error message: you can’t index an Array with a Symbol.
I think you want to do something more like this:
That will give you a structure like this: