从Ruby on Rails中的嵌套参数中获取值

时间:2015-08-04 04:39:28

标签: jquery ruby-on-rails ruby datatables

我在Ruby on Rails中使用jQuery DataTables插件,并且我在检索嵌套参数上的值时遇到问题。

当我尝试使用

进行检查时
puts params.inspect

我在控制台中获得了以下内容

Parameters: {"draw"=>"1", "columns"=>{"0"=>{"data"=>"0", "name"=>"", "searchable"=>"false", "orderable"=>"false", "search"=>{"value"=>"", "regex"=>"false"}}, "1"=>{"data"=>"1", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, "2"=>{"data"=>"2", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, "3"=>{"data"=>"3", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, "4"=>{"data"=>"4", "name"=>"", "searchable"=>"false", "orderable"=>"false", "search"=>{"value"=>"", "regex"=>"false"}}}, "order"=>{"0"=>{"column"=>"1", "dir"=>"asc"}}, "start"=>"0", "length"=>"10", "search"=>{"value"=>"", "regex"=>"false"}, "_"=>"1438662290344"}

我想从以下地址获取价值:订单,我将我的代码更改为跟随

puts params[:order].inspect

然后我在我的控制台中得到以下内容

{"0"=>{"column"=>"1", "dir"=>"asc"}}

我想获得" dir" =>" asc"的价值,我尝试了以下代码,但仍然没有运气

puts params[:order][0][:dir].inspect
puts params["order"][0]["dir"].inspect

非常感谢任何建议,谢谢。

1 个答案:

答案 0 :(得分:0)

首先需要以实际可以看到结构的方式格式化params-hash。像这样:

{
    "draw"=>"1", 
    "columns"=>{
         "0"=>{"data"=>"0", "name"=>"", "searchable"=>"false", "orderable"=>"false", "search"=>{"value"=>"", "regex"=>"false"}}, 
         "1"=>{"data"=>"1", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, 
         "2"=>{"data"=>"2", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, 
         "3"=>{"data"=>"3", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, 
         "4"=>{"data"=>"4", "name"=>"", "searchable"=>"false", "orderable"=>"false", "search"=>{"value"=>"", "regex"=>"false"}}
   }, 
   "order"=>{
       "0"=>{"column"=>"1", "dir"=>"asc"}}, 
       "start"=>"0", 
       "length"=>"10", 
       "search"=>{"value"=>"", "regex"=>"false"}, 
    "_"=>"1438662290344"}
}

现在很清楚:你有字符串作为键,也有数字,你应该通过使用获得所需的信息:

params["order"]["0"]["dir"]