简体   繁体   English

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

[英]Fetching value from nested params in Ruby on Rails

I use jQuery DataTables plugin with Ruby on Rails, and I'm having problem with retrieving the value on the nested params. 我将jQuery DataTables插件与Ruby on Rails结合使用,并且在检索嵌套参数上的值时遇到问题。

When I tried to inspect using 当我尝试检查使用

puts params.inspect

I got the following in my console 我的控制台中有以下内容

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"}

I want to get the value from :order, I changed my code to be like following 我想从:order获取值,我将代码更改如下

puts params[:order].inspect

Then I got the following in my console 然后我在控制台中得到以下内容

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

I wanna get the value of "dir"=>"asc", I tried the following codes but still no luck 我想获取“ dir” =>“ asc”的值,我尝试了以下代码,但还是没有运气

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

Any advice would be much appreciated, thanks. 任何建议将不胜感激,谢谢。

You first need to format the params-hash in way that you can actually see the structure. 您首先需要以可以实际看到结构的方式来格式化params-hash。 Like this: 像这样:

{
    "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"}
}

Now it is quite clear: You have strings as keys, also for the numbers, and you should get the information you need by using: 现在很清楚:您将字符串作为键,也将数字用作键,并且应该使用以下方法获取所需的信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM