I have
item_params = {
"name" => "",
"description" => "",
"tag_list" => "",
"user_items_attributes" => {"0" => {"user_id" => "8"}},
"created_by" => 8,
"status" => 0
}
and I want to access the user_id to change it.
I tried params[:item][:user_items_attributes][0]
and it doesn't work. I also tried params[:item][:user_items_attributes][0][:user_id]
. What is the correct way to change the user_id
?
The value of params[:item][:user_items_attributes]
is a hash mapping a string to a hash. You're trying to access it using an integer 0
instead of '0'
:
params[:item][:user_items_attributes]['0']
=> {"user_id"=>"8"}
You can often access hashes using symbol keys rather than the string keys that will display if you inspect the hash because of rails' HashWithIndifferentAccess but you'll need to use a string for the key in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With