default_url_options and rails 3

gucki picture gucki · May 23, 2011 · Viewed 24.5k times · Source

As ActionController::Base#default_url_options is deprecated, I wonder how to set default url options in rails3. The default url options are not static but dependent of the current request.

http://apidock.com/rails/ActionController/Base/default_url_options

Thanks, Corin

Answer

Lukasz Sliwa picture Lukasz Sliwa · Jun 5, 2011

To set url options for current request use something like this in your controller:

class ApplicationController < ActionController::Base

  def url_options
    { :profile => current_profile }.merge(super)
  end

end

Now, :profile => current_profile will be automerge to path/url parameters.

Example routing:

scope ":profile" do
  resources :comments
end

Just write:

comments_path

and if current_profile has set to_param to 'lucas':

/lucas/comments