将查询字符串中的多维数组传递给Rails中的Controller

时间:2012-09-12 23:08:27

标签: ruby-on-rails controller query-parameters

将多维数组传递给rails控制器时,似乎无法正确解析。我做错了吗?

url: http://localhost:3000/people?sort[][]=lastname&sort[][]=1&sort[][]=firstname&sort[][]=1
params: {
          "sort" => [
        [0] nil,
        [1] nil,
        [2] nil,
        [3] nil
    ],
        "action" => "index",
    "controller" => "people"
}

应该是:

params: {
          "sort" => [
        [0] => [
          [0] => 'lastname',
          [1] => 1
        ],
        [1] = > [
          [0] => 'firstname',
          [1] => 1
        ]
    ],
        "action" => "index",
    "controller" => "people"
}

1 个答案:

答案 0 :(得分:7)

Rails不支持查询字符串中的多维数组。

它支持一维数组:

http://localhost:3000/people?sort[]=lastname&sort[]=firstname
# params[:sort] == ['lastname', 'firstname']

并且还支持哈希:

http://localhost:3000/people?sort[lastname]=asc&sort[firstname]=desc
# params[:sort] == {:lastname => 'asc', :firstname => 'desc'}