Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default_url_options and rails 3

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

like image 445
gucki Avatar asked May 23 '11 17:05

gucki


3 Answers

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
like image 138
Lukasz Sliwa Avatar answered Nov 17 '22 18:11

Lukasz Sliwa


I believe the preferred method is to now tell the router to handle this:

Rails.application.routes.default_url_options[:foo]= 'bar' 

You can put this line in either routes.rb or an initializer. Whichever you would prefer. You could even put it in your environment configs if the values change based on your environment.

like image 24
Dylan Markow Avatar answered Nov 17 '22 17:11

Dylan Markow


That apidock.com link is misleading. default_url_options is not deprecated.

http://guides.rubyonrails.org/action_controller_overview.html#default_url_options

like image 4
Jason Heiss Avatar answered Nov 17 '22 17:11

Jason Heiss