Rails 5 GET request nested array parameter changes unexpectedly

*爱你&永不变心* 提交于 2020-04-17 19:05:18

问题


I have upgraded a Rails 4 application to Rails 5 (5.2.4.1 to be exact).

I have a GET endpoint in my code with an rspec test.

The test code sends the request as:

get :fetch, params: { id: 123, logs: [[1, 2, :weekly], [1, 4, :mothly]] }

When the controller receives the request, params['logs'] contains

[["1"], ["2"], ["weekly"], ["1"], ["4"], ["monthly"]]

This is different from what I expected, which is

[["1", "2", "weekly"], ["1", "4", "monthly"]]

The incoming URL contains (after decoding the URL): ?id=123&logs[][]=1&logs[][]=2&logs[][]=weekly&logs[][]=1&logs[][]=4&logs[][]=monthly

This is the same URL in Rails 4, but it worked somehow. Was something else being passed? In any case, it's no longer working Rails 5. The Rails doc https://edgeguides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters does not mention nested array parameters.

Is it possible to make this work with Rails 5? Is there any quick workaround?

The problem could be with rack gem. The two versions used are: 1.6.13 and 2.2.2.

(I can send the whole structure as a json string in one parameter; I know this is an alternative solution. So please don't propose this as a solution.)

Thank you.

来源:https://stackoverflow.com/questions/60930886/rails-5-get-request-nested-array-parameter-changes-unexpectedly

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