Pass Multidimensional Array in Query String to Controller in Rails

为君一笑 提交于 2019-12-23 20:45:17

问题


When passing a multidimensional array to a rails controller, it does not seem to parse correctly. Am I doing it wrong?

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

should be:

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

回答1:


Rails doesn't support multi-dimensional arrays in the query string.

It supports one-dimensional arrays:

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

and also supports hashes:

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


来源:https://stackoverflow.com/questions/12397276/pass-multidimensional-array-in-query-string-to-controller-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!