English 中文(简体)
如同在鲁比拉发生的铁路拖车一样
原标题:Parse a string as if it were a querystring in Ruby on Rails

我这样说:

"foo=bar&bar=foo&hello=hi"

鲁比拉铁路公司是否提供穿透的方法,就像它是一种争,一样,因此,我得到的就是:

{
    :foo => "bar",
    :bar => "foo",
    :hello => "hi"
}

还是我自己写了吗?

EDIT

请注意,以上所述并不是来自URL的真正询问,而是存放在FacebookLink的一个 co子里的扼杀。

最佳回答

答案取决于你使用的铁路。 如果你使用2.3或以后,则使用Rack built子管子

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

如果你在旧铁路上,你确实可以使用<条码>:parse<>/code>。 请注意,处理海螺和阵列的方式在单元之间有所不同,因此,你需要核实你获得的数据是否适合你选择的方法。

也可将<代码>Rack:Utils列入您的短期访问类别。

问题回答

The

CGI::parse("foo=bar&bar=foo&hello=hi")

页: 1

{"foo"=>["bar"], "hello"=>["hi"], "bar"=>["foo"]}

Edit: As specified by Ryan Long this version accounts for multiple values of the same key, which is useful if you want to parse arrays too.

Edit:

As Ben points out, this may not handle arrays well when they are formatted with ruby on rails style array notation. The rails style array notation is: foo[]=bar&foo[]=nop. That style is indeed handled correctly with Julik s response.

如果你有“条形码”和“foo=bar&foo=nop

<>Edit :,如评论所述,如果有人想要伤害你,则拆除钥匙可降低服务器。 在我处理低姿态时,我仍然做了很多工作,因为它使事情更容易地与大家合作,但我不会因为高利害关系而做。

不要忘记象征取得你想要的结果的关键

Rack::Utils.parse_nested_query("a=2&b=tralalala").deep_symbolize_keys

这项行动对重复工作具有破坏性。

如果你谈论正在用来获取有关参数的数据的乌拉尔

> request.url
=> "http://localhost:3000/restaurants/lokesh-dhaba?data=some&more=thisIsMore"

接下来是得出问题参数。 用途

> request.query_parameters
=> {"data"=>"some", "more"=>"thisIsMore"}

如果你想要洗衣,你可以使用

Hash[CGI::parse(x).map{|k,v| [k, v.first]}]




相关问题
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: ...

热门标签