url_for params


url_for has a nice way of using the current 'params' object to fill in default
parameters in a route. For example,

map.connect ':controller/:action/:id/'

with <%= url_for :action => 'my_action' %> will redirect to the current
controller. All very good.

However, I sometimes want a url_for() which includes current parameters which
aren't in the route, for example I might want a link to preserve ?order=name in
the query string.

I've been using url_for(params.merge({:new_param => new_value})), but there's a
problem with controllers in modules

# params == {:controller => 'sub/controller', :action => 'action'}
url_for params.merge({:new_param => new_value})
# ==> /sub/sub/controller/action?new_param=new_value

to fix the problem I have to put

params.merge({:controller => "/#{params[:controller]}"}).merge({:new_param =>

My question is, is there an easier way to do this? And if not, why is
params[:controller] not anchored with a '/' to start with?


I can answer part of this:

params[:controller] doesn’t have a ‘/’ prefix because it’s a parameter.

Just like params[:id]

The URL is actually something like


Routes.rb addes the slashes.