Search code examples
ruby-on-railsrubyhashquery-stringurldecode

How do I turn a query string into a hash?


I’m using Ruby on Rails 4.2.7. How do I take an encoded query string, like

submitbutton=View&a=b&d=%26%26

and turn it into a hash of name value pairs in which the values are url-unencoded?


Solution

  • You can use CGI.parse

    CGI.parse('submitbutton=View&a=b&d=%26%26')
    #=> {"submitbutton"=>["View"], "a"=>["b"], "d"=>["&&"]}