【发布时间】:2014-11-19 10:57:17
【问题描述】:
我想知道从参数中提取哈希值的更好方法。 参数如下所示(记住我的参数名称是 params[:ad_template]):
{"name"=>"name",
"description"=>"description",
"tag_list"=>"Software Development",
"objective"=>"VIDEO_VIEW",
"ad_creative_templates_attributes"=>{
"0"=>{"creative_template_id"=>"2430", "_destroy"=>"false"}
},
"ad_target_templates_attributes"=>{
"0"=>{"target_template_id"=>"1526", "_destroy"=>"false"}
}
}
现在我想从这个参数中获取“creative_template_id”的值。
我可以通过执行下面的代码来获得这个值:
params[:ad_template][:ad_creative_templates_attributes].first.second[:creative_template_id]
这给了我正确的值 2430,但我不喜欢这种方法。 所以我想知道从参数中提取该值的更好解决方案。
谢谢。
【问题讨论】:
-
您可以在深度哈希中搜索特定键的值,例如:stackoverflow.com/questions/15031412/…
标签: ruby ruby-on-rails-3 hash params