简体   繁体   English

命名相同的Rails多个参数

[英]Rails Multiple Params Named the same

I'm working with a web service that will route a user back to my site and may append parameters about the user (I know this is poor form, but they send multiple parameters back that are the same parameter with different data). 我正在使用Web服务,该服务会将用户路由回我的网站,并可能附加有关用户的参数(我知道这是较差的形式,但是它们会向后发送多个参数,这些参数是具有不同数据的相同参数)。 Say they collected information about what kinds of pets the user has. 假设他们收集了有关用户拥有哪种宠物的信息。 They'd send it back in the format of: 他们将以以下格式发送回:

&STANDARD_PETS=1&STANDARD_PETS=3&STANDARD_PETS=4&STANDARD_PETS=8

In Rails, we seem to only grab the final parameter. 在Rails中,我们似乎只抓住了最后一个参数。 So I see parameters: {"STANDARD_PETS"=>"8"} 所以我看到了parameters: {"STANDARD_PETS"=>"8"}

Is there any way in rails to also get the other 3 STANDARD_PETS parameters that are passed in? Rails中是否有办法也可以获取传入的其他3个STANDARD_PETS参数?

require 'uri'

url = 'http://www.foo.com?&STANDARD_PETS=1&STANDARD_PETS=3&STANDARD_PETS=4&STANDARD_PETS=8'
CGI.parse(URI.parse(url).query)
#=> {"STANDARD_PETS"=>["1", "3", "4", "8"]}

To supplement @MilesStanfield's answer 补充@MilesStanfield的答案

You can use request.original_url to get access to the full url (including parameters) 您可以使用request.original_url来访问完整的URL(包括参数)

require 'uri'
good_params = CGI.parse(URI.parse(request.original_url).query)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM