Tuesday, March 02, 2010

Convert a Hash to a Query String or Parameter String

I Rails, to convert a Hash object into a query string or parameter string for use in a url:

opts = {:a => 1, :b => 2}
params = ActionController::Routing::Route.new.build_query_string(opts)

Params => "?a=1&b=2"

1 comment:

Jan said...

or just use { :a => 'b' }.to_query (or the alias to_param if you prefer).