如何从Rails的URL获取查询字符串?

时间:2023-02-05 20:21:22

Is there is a way to get the query string in a passed URL string in Rails?

是否有一种方法可以在Rails中传递的URL字符串中获取查询字符串?

I want to pass a URL string:

我想传递一个URL字符串:

http://www.foo.com?id=4&empid=6

How can I get id and empid?

我怎样才能得到id和empid?

5 个解决方案

#1


70  

If you have a URL in a string then use URI and CGI to pull it apart:

如果你有一个URL在一个字符串,然后使用URI和CGI把它分开:

url    = 'http://www.foo.com?id=4&empid=6'
uri    = URI.parse(url)
params = CGI.parse(uri.query)
# params is now {"id"=>["4"], "empid"=>["6"]}

id     = params['id'].first
# id is now "4"

Please use the standard libraries for this stuff, don't try and do it yourself with regular expressions.

请使用这些标准库,不要尝试自己使用正则表达式。

References:

引用:

#2


26  

In a Ruby on Rails controller method the URL parameters are available in a hash called params, where the keys are the parameter names, but as Ruby "symbols" (ie. prefixed by a colon). So in your example, params[:id] would equal 4 and params[:empid] would equal 6.

在Ruby on Rails控制器方法中,URL参数可以在一个名为params的散列中获得,其中的关键字是参数名,但作为Ruby的“符号”。前缀冒号)。在你的例子中,params[:id]等于4,params[:empid]等于6。

I would recommend reading a good Rails tutorial which should cover basics like this. Here's one example - google will turn up plenty more:

我建议您阅读一篇好的Rails教程,它应该涵盖像这样的基础知识。这里有一个例子——谷歌会出现更多:

#3


19  

vars = request.query_parameters
vars['id']
vars['empid']

etc..

等。

#4


14  

Rack::Utils.parse_nested_query("a=2") #=> {"a" => "2"}

quoted from: Parse a string as if it were a querystring in Ruby on Rails

引用自:将字符串解析为Ruby on Rails中的查询字符串

Parse query strings the way rails controllers do. Nested queries, typically via a form field name like this lil guy: name="awesome[beer][chips]" # => "?awesome%5Bbeer%5D%5Bchips%5D=cool", get 'sliced-and-diced' into an awesome hash: {"awesome"=>{"beer"=>{"chips"=>nil}}}

像rails控制器那样解析查询字符串。嵌套查询,通常是通过这样的表单字段名:name="awesome[beer][chips]" # => "?超棒的啤酒,超棒的薯片,超棒的薯片,超棒的薯片,超棒的薯片,超棒的薯片,超棒的薯片

http://rubydoc.info/github/rack/rack/master/Rack/Utils.parse_nested_query https://github.com/rack/rack/blob/master/lib/rack/utils.rb#L90

http://rubydoc.info/github/rack/rack/master/Rack/Utils.parse_nested_query https://github.com/rack/rack/blob/master/lib/rack/utils.rb L90

#5


0  

This is not the best method, but it works:

这不是最好的方法,但它能起作用:

request.query_string.split(/&/).inject({}) do |hash, setting|
  key, val = setting.split(/=/)
  hash[key.to_sym] = val
  hash
end

This will return hash with all GET params( :name => value ). Or just use request.query_string method, depends on in which format you want to get your GET params. Also you can use request.query_params from rake gem.

这将返回所有GET params(:name =>值)的散列。或只使用请求。query_string方法,取决于您希望获取get参数的格式。你也可以使用请求。从耙query_params宝石。

#1


70  

If you have a URL in a string then use URI and CGI to pull it apart:

如果你有一个URL在一个字符串,然后使用URI和CGI把它分开:

url    = 'http://www.foo.com?id=4&empid=6'
uri    = URI.parse(url)
params = CGI.parse(uri.query)
# params is now {"id"=>["4"], "empid"=>["6"]}

id     = params['id'].first
# id is now "4"

Please use the standard libraries for this stuff, don't try and do it yourself with regular expressions.

请使用这些标准库,不要尝试自己使用正则表达式。

References:

引用:

#2


26  

In a Ruby on Rails controller method the URL parameters are available in a hash called params, where the keys are the parameter names, but as Ruby "symbols" (ie. prefixed by a colon). So in your example, params[:id] would equal 4 and params[:empid] would equal 6.

在Ruby on Rails控制器方法中,URL参数可以在一个名为params的散列中获得,其中的关键字是参数名,但作为Ruby的“符号”。前缀冒号)。在你的例子中,params[:id]等于4,params[:empid]等于6。

I would recommend reading a good Rails tutorial which should cover basics like this. Here's one example - google will turn up plenty more:

我建议您阅读一篇好的Rails教程,它应该涵盖像这样的基础知识。这里有一个例子——谷歌会出现更多:

#3


19  

vars = request.query_parameters
vars['id']
vars['empid']

etc..

等。

#4


14  

Rack::Utils.parse_nested_query("a=2") #=> {"a" => "2"}

quoted from: Parse a string as if it were a querystring in Ruby on Rails

引用自:将字符串解析为Ruby on Rails中的查询字符串

Parse query strings the way rails controllers do. Nested queries, typically via a form field name like this lil guy: name="awesome[beer][chips]" # => "?awesome%5Bbeer%5D%5Bchips%5D=cool", get 'sliced-and-diced' into an awesome hash: {"awesome"=>{"beer"=>{"chips"=>nil}}}

像rails控制器那样解析查询字符串。嵌套查询,通常是通过这样的表单字段名:name="awesome[beer][chips]" # => "?超棒的啤酒,超棒的薯片,超棒的薯片,超棒的薯片,超棒的薯片,超棒的薯片,超棒的薯片

http://rubydoc.info/github/rack/rack/master/Rack/Utils.parse_nested_query https://github.com/rack/rack/blob/master/lib/rack/utils.rb#L90

http://rubydoc.info/github/rack/rack/master/Rack/Utils.parse_nested_query https://github.com/rack/rack/blob/master/lib/rack/utils.rb L90

#5


0  

This is not the best method, but it works:

这不是最好的方法,但它能起作用:

request.query_string.split(/&/).inject({}) do |hash, setting|
  key, val = setting.split(/=/)
  hash[key.to_sym] = val
  hash
end

This will return hash with all GET params( :name => value ). Or just use request.query_string method, depends on in which format you want to get your GET params. Also you can use request.query_params from rake gem.

这将返回所有GET params(:name =>值)的散列。或只使用请求。query_string方法,取决于您希望获取get参数的格式。你也可以使用请求。从耙query_params宝石。