【问题标题】:getting value of hash from params从参数中获取哈希值
【发布时间】: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,但我不喜欢这种方法。 所以我想知道从参数中提取该值的更好解决方案。

谢谢。

【问题讨论】:

标签: ruby ruby-on-rails-3 hash params


【解决方案1】:

怎么样:

params[:ad_template][:ad_creative_templates_attributes]['0'][:creative_template_id]

【讨论】:

  • 感谢@Uri Agassi 的解决方案。我想知道这是最好的解决方案。
猜你喜欢
  • 1970-01-01
  • 2016-07-22
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2017-02-16
相关资源
最近更新 更多