3

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 Answer 1

7

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'}
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be supported in Rails 4, but not anymore in Rails 5. stackoverflow.com/questions/60930886/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.