Constructing URL parameters in Rails 3
I am trying to use the following code to convert a hash of options (e.g. { :opt1 => 'a', :opt2 => 'b' } ) into parameters to be appended to a URL (e.g. example.com/page?opt1=a&opt2=b):
ActionController::Routing::Route.new.build_query_string(options)
where options is the hash. I get the following error:
wrong number of arguments (0 for 7)
The corresponding docs show that the initializer method for Route take no arguments, but I think they haven't been updated for Rails 3 yet. I know routing has seen a big update for Rails 3, so I assume开发者_如何学Go this code is for Rails 2.x. Does anyone know what the Rails 3 way of doing this would be?
Thanks
Never mind - I found the answer.
Rails provides a Hash#to_query method (and for some other types too I think).
Somehow Google did a poor job of finding this for me!
精彩评论