【发布时间】:2012-03-20 05:14:48
【问题描述】:
如何循环访问控制器中的参数以便更改它们的值?
这个:
...
params[:contact][:relationships_attributes].each do |rel|
rel[:contact_ids] = [rel[:contact_ids]]
end
...
返回错误:
# can't convert Symbol into Integer
为了提供更多上下文,我使用单个选择框代替多选。所以控制器期待一个数组,但它得到一个单一的值。
这是我的参数:
{"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"
}
【问题讨论】:
-
@muistooshort 我更新了帖子以包含我的参数。
标签: ruby-on-rails ruby ruby-on-rails-3 hash