如何使用 Ruby 或 Rails 从 URL 中提取 URL 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2500462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:26:09  来源:igfitidea点击:

How to extract URL parameters from a URL with Ruby or Rails?

ruby-on-railsrubyuriurl-parameters

提问by Flackou

I have some URLs, like

我有一些网址,例如

http://www.example.com/something?param1=value1&param2=value2&param3=value3

and I would like to extract the parameters from these URLs and get them in a Hash. Obviously, I could use regular expressions, but I was just wondering if there was easier ways to do that with Ruby or Rails. I haven't found anything in the Ruby module URIbut perhaps I missed something.

我想从这些 URL 中提取参数并将它们放入哈希中。显然,我可以使用正则表达式,但我只是想知道使用 Ruby 或 Rails 是否有更简单的方法来做到这一点。我在 Ruby 模块中没有找到任何东西,URI但也许我错过了一些东西。

In fact, I need a method that would do that:

事实上,我需要一种方法来做到这一点:

extract_parameters_from_url("http://www.example.com/something?param1=value1&param2=value2&param3=value3")
#=> {:param1 => 'value1', :param2 => 'value2', :param3 => 'value3'}

Would you have some advices?

你有什么建议吗?

回答by Sniper

I think you want to turn any given URL string into a HASH?

我认为您想将任何给定的 URL 字符串转换为 HASH?

You can try http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075

你可以试试http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075

require 'cgi'

CGI::parse('param1=value1&param2=value2&param3=value3')

returns

回报

{"param1"=>["value1"], "param2"=>["value2"], "param3"=>["value3"]}

回答by Arthur Gunn

I found myself needing the same thing for a recent project. Building on Levi's solution, here's a cleaner and faster method:

我发现自己最近的一个项目需要同样的东西。基于 Levi 的解决方案,这里有一个更干净、更快捷的方法:

Rack::Utils.parse_nested_query 'param1=value1&param2=value2&param3=value3'
# => {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3"}

回答by rtdp

Just Improved with Levi answer above -

刚刚改进了上面的 Levi 回答 -

Rack::Utils.parse_query URI("http://example.com?par=hello&par2=bye").query

For a string like above url, it will return

对于像上面的 url 这样的字符串,它会返回

{ "par" => "hello", "par2" => "bye" } 

回答by alup

For a pure Ruby solution combine URI.parsewith CGI.parse(this can be used even if Rails/Rack etc. are not required):

对于纯 Ruby 解决方案,结合URI.parse使用CGI.parse(即使不需要 Rails/Rack 等也可以使用):

CGI.parse(URI.parse(url).query) 
# =>  {"name1" => ["value1"], "name2" => ["value1", "value2", ...] }

回答by Arup Rakshit

There more than one ways, to solve your problem. Others has shown you the some tricks. I know another trick. Here is my try :-

有不止一种方法可以解决您的问题。其他人已经向您展示了一些技巧。我知道另一个技巧。这是我的尝试:-

require 'uri'
url = "http://www.example.com/something?param1=value1&param2=value2&param3=value3"
uri = URI(url)
# => #<URI::HTTP:0x89e4898 URL:http://www.example.com/something?param1=value1&param2=value2&param3=value3>
URI::decode_www_form(uri.query).to_h # if you are in 2.1 or later version of Ruby
# => {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3"}
Hash[URI::decode_www_form(uri.query)] # if you are below 2.1 version of Ruby
# => {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3"}

Read the method docomentation of ::decode_www_form.

阅读方法文档::decode_www_form

回答by Yarin

Check out the addressablegem - a popular replacement for Ruby's URI module that makes query parsing easy:

查看可寻址gem - Ruby URI 模块的流行替代品,使查询解析变得容易:

require "addressable/uri"
uri = Addressable::URI.parse("http://www.example.com/something?param1=value1&param2=value2&param3=value3")
uri.query_values['param1']
=> 'value1'

(It also apparently handles param encoding/decoding, unlike URI)

(它显然也处理参数编码/解码,与 URI 不同)

回答by kafuchau

In your Controller, you should be able to access a dictionary (hash) called params. So, if you know what the names of each query parameter is, then just do params[:param1]to access it... If you don't know what the names of the parameters are, you could traverse the dictionary and get the keys.

在您的控制器中,您应该能够访问名为params. 因此,如果您知道每个查询参数的名称,那么只需params[:param1]访问它...如果您不知道参数的名称,则可以遍历字典并获取键。

Some simple examples here.

这里有一些简单的例子。