How to generate a Rails-compatible query string

From Rails 3.0.9, there is a method Hash#to_query that will turn a Hash into a query string:

>> {:a => "a", :b => ["c", "d", "e"]}.to_query
=> "a=a&b%5B%5D=c&b%5B%5D=d&b%5B%5D=e"
>> CGI.unescape _
=> "a=a&b[]=c&b[]=d&b[]=e"

If you're on the browser side, you can serialize nestd objects to query strings using jQuery's $.param Show archive.org snapshot .

Dominik Schöler Over 8 years ago