English 中文(简体)
如何从与Ruby或铁路的URL中提取URL参数?
原标题:How to extract URL parameters from a URL with Ruby or Rails?

我有一些URLs。

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

并且我要从这些URLs中提取这些参数,并将其输入哈希。 显然,我可以使用定期的措辞,但我很想知道,与Ruby或铁路公司相比,这样做是否容易。 我没有发现“鲁比”模块“URI

事实上,我需要一种方法:

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

你们是否有一些建议?

最佳回答

我认为,你想要把任何特定的“URL”扼杀变成“讲卫生运动”。

http://www.ruby-doc.org/stdlib/doc/cgi/rdoc/classes/CGI.html#M000075"rel=“noretinger” http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075

require  cgi 

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

回返

{"param1"=>["value1"], "param2"=>["value2"], "param3"=>["value3"]}
问题回答

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

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

见上文“莱维特”的答复:

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

为了像上面的ur,它将返回。

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

纯粹的Ruby将URI.parseCGI.parse <>code>合并。 (即使不需要铁路/铁路等,也可使用):

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

解决你的问题有多种途径。 其他人向你们展示了一些trick。 我知道另一个骗局。 在此,我尝试:

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"}

http://www.ruby-doc.org/stdlib-2.1.0/libdoc/uri/rdoc/URI.html#method-c-decode_www_form”rel=“noreferer”>:decode_www_form

使用国际地理信息公司可能是一种过时的做法,与Ruby 2.7/3相同。

在这里,与世界人权学会一道采取这一步骤:

uri = URI.parse  https://duckduckgo.com/?q=ruby+programming+language 
params = Hash[URI.decode_www_form uri.query]
# => {"q"=>"ruby programming language"} 

也可以使用这种方法。


require  uri 
require  cgi 
uri = URI("https://example.com/?query=1&q=2&query=5")
a = CGI::parse(uri.query)
puts a                   #=> {"query"=>["1", "5"], "q"=>["2"]}
puts a["query"].to_s     #=> ["1", "5"]
puts a["query"][0]       #=>  1
puts a["query"][1]       #=>  5
puts a["q"][0]           #=>  2


安全和更方便

可悲的是, 可处理的图书馆在试图从“URLs”中提取电灯泡时断裂。 比如,这两者之间的区别是:

http://localhost:4300/webapp/foo/#//controller/action?account=001-001-111&email=john%40email.com

以阿瑟/莱克斯解决方案为基础,url.split(......)” 您可以只读一下URL的问答部分,并使用<代码>。 Rack:Utils.parse_nested_query to parse that string of factors into ah.

或完全:

Rack::Utils.parse_nested_query(url.split("?").try(:last))

我的榜样是:

{"account": "001-001-111", "email": "john@email.com"}




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签