我有一个包含有关应用程序信息的params哈希。这个应用程序与照片有很多关系。
鉴于特定条件,我需要循环遍历哈希并更改应用程序哈希中每个照片哈希值中的一个值。我需要能够在创建哈希之后进行此更改。
这是一个例子。
我正在尝试在app_params中将每张照片中的keep_photo属性更改为true。
app_params = {"title"=>"Tests", "category_id"=>"1", ...... ", "SFDC_package_url"=>"", "photos_attributes"=>
{"0"=>{"id"=>"45", "name"=>"d", "remote_image_url"=>"", "_destroy"=>"false", "keep_photo"=>"false"},
"1"=>{"id"=>"46", "name"=>"g", "remote_image_url"=>"", "_destroy"=>"false", "keep_photo"=>"false"},
"2"=>{"id"=>"47", "name"=>"guy", "remote_image_url"=>"", "_destroy"=>"false", "keep_photo"=>"false"}}}
我该怎么做?
答案 0 :(得分:0)
使用此:
app_params["photos_attributes"].values.map! { |x| x["keep_photo"] = "true"}
这会将app_params
更新为:
app_params = {"title"=>"Tests", "category_id"=>"1", ...... ", "SFDC_package_url"=>"", "photos_attributes"=>
{"0"=>{"id"=>"45", "name"=>"d", "remote_image_url"=>"", "_destroy"=>"false", "keep_photo"=>"true"},
"1"=>{"id"=>"46", "name"=>"g", "remote_image_url"=>"", "_destroy"=>"false", "keep_photo"=>"true"},
"2"=>{"id"=>"47", "name"=>"guy", "remote_image_url"=>"", "_destroy"=>"false", "keep_photo"=>"true"}}}
如果它在你的手中,那么我仍然建议你首先传递正确的值,即设置正确的值"keep_photo" => "true"
以产生app_params
本身。所以,你可以避免这些额外的改变。