如何使用 Ruby 或 Rails 从 URL 中提取 URL 参数?
我有一些 URL,例如
http://www.example.com/something?param1=value1¶m2=value2¶m3=value3
,我想从这些 URL 中提取参数并将它们放入哈希中。显然,我可以使用正则表达式,但我只是想知道是否有更简单的方法可以使用 Ruby 或 Rails 来做到这一点。我没有在 Ruby 模块 URI
中找到任何内容,但也许我错过了一些东西。
事实上,我需要一种方法来做到这一点:
extract_parameters_from_url("http://www.example.com/something?param1=value1¶m2=value2¶m3=value3")
#=> {:param1 => 'value1', :param2 => 'value2', :param3 => 'value3'}
你有一些建议吗?
I have some URLs, like
http://www.example.com/something?param1=value1¶m2=value2¶m3=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 URI
but perhaps I missed something.
In fact, I need a method that would do that:
extract_parameters_from_url("http://www.example.com/something?param1=value1¶m2=value2¶m3=value3")
#=> {:param1 => 'value1', :param2 => 'value2', :param3 => 'value3'}
Would you have some advices?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
对于 Ruby 2.7/3,使用 CGI 可能是一种过时的方法。
这是使用 URI 执行此操作的巧妙方法:
Using CGI might be an outdated approach with Ruby 2.7/3.
Here's a neat way to do this with URI:
您也可以使用此方法,
它安全且更容易
you can also use this method
its safe and much easier
遗憾的是,在尝试从有问题的 URL 中提取查询参数时,
URI
和addressable
库都会崩溃。例如,这会破坏两者:构建在 Arthur / Levi 的解决方案上,使用
url.split("?").try(:last)
您可以仅获取 URL 的查询参数部分,并使用Rack::Utils.parse_nested_query
将参数字符串解析为哈希值。或完整:
在我的示例中返回:
Sadly both the
URI
andaddressable
libraries break when attempting to extract query params from buggy URLs. E.g. this breaks both:Building on Arthur / Levi's solution, with
url.split("?").try(:last)
you can grab just the query param portion of the URL, and useRack::Utils.parse_nested_query
to parse that string of parameters into a hash.Or in full:
returning in my example:
上面的答案写得很好并且达到了目的,但是我尝试了一些查询操作并想在这里分享。所以,这是我的看法:
它达到目的并为每个查询参数返回一个对象数组。话虽如此,请记住ROR的COC原则。
The above answers are very well written and they serve the purpose, however I tried some query manipulation and wanted to share here. So, here is my take:
It serves the purpose and return an array of objects for each query param. Having said that, please keep in mind the COC principle of ROR.
在您的控制器中,您应该能够访问名为
params
的字典(哈希)。因此,如果您知道每个查询参数的名称是什么,那么只需执行params[:param1]
即可访问它...如果您不知道参数的名称是什么,您可以可以遍历字典并获取密钥。此处有一些简单的示例。
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 doparams[: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.Some simple examples here.
我想你想将任何给定的 URL 字符串转换为 HASH?
您可以尝试 http://www.ruby- doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075
返回
I think you want to turn any given URL string into a HASH?
You can try http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075
returns
我发现自己在最近的项目中需要同样的东西。
基于 Levi's 解决方案,这里有一个更干净、更快速的方法:
I found myself needing the same thing for a recent project.
Building on Levi's solution, here's a cleaner and faster method:
只是根据上面的 Levi 答案进行了改进 -
对于像上面 url 这样的字符串,它将返回
Just Improved with Levi answer above -
For a string like above url, it will return
对于纯 Ruby 解决方案,将
URI.parse
与CGI.parse
结合起来(即使不需要 Rails/Rack 等,也可以使用):For a pure Ruby solution combine
URI.parse
withCGI.parse
(this can be used even if Rails/Rack etc. are not required):有不止一种方法可以解决您的问题。其他人已经向您展示了一些技巧。我还知道另一个窍门。这是我的尝试:-
阅读
::decode_www_form
。There more than one ways, to solve your problem. Others has shown you the some tricks. I know another trick. Here is my try :-
Read the method docomentation of
::decode_www_form
.查看 addressable gem - Ruby URI 模块的流行替代品,使查询解析变得容易:(
它显然也处理参数编码/解码,与 URI 不同)
Check out the addressable gem - a popular replacement for Ruby's URI module that makes query parsing easy:
(It also apparently handles param encoding/decoding, unlike URI)