Rails - Parameter with multiple values in the URL

2019-03-18 09:31发布

I am consuming an API that expects me to do requests in the following format:

?filter=value1&filter=value2

However, I am using Active Resource and when I specify the :params hash, I can't make the same parameter to appear twice in the URL, which I believe is correct. So I can't do this:

:params => {:consumer_id => self.id, :filter => "value1", :filter => "value2" }, because the second filter index of the hash will be ignored.

I know I can pass an array (which I believe is the correct way of doing it) like this:

:params => {:consumer_id => self.id, :filter => ["value1","value2"] }

Which will produce a URL like:

?filter[]=value1&filter[]=value2

Which to me seems ok, but the API is not accepting it. So my question are:

What is the correct way of passing parameters with multiple values? Is it language specific? Who decides this?

2条回答
Juvenile、少年°
2楼-- · 2019-03-18 09:39

to create a valid query string, you can use

params = {a: 1, b: [1,2]}.to_query

http://apidock.com/rails/Hash/to_query
http://apidock.com/rails/Hash/to_param

查看更多
登录 后发表回答