default_url_options and rails 3
Asked Answered
J

5

17

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

Jaundiced answered 23/5, 2011 at 17:54 Comment(1)
ActionController::Base#default_url_options is not deprecated.Impressure
P
26

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
Polonaise answered 4/6, 2011 at 22:9 Comment(7)
Thanks! Is this the "official" rails 3.0/3.1 way?Jaundiced
Too bad, it throws an exception when it's declared as protected which is a must (otherwise it's exposed as an action). So it does not work.Jaundiced
It works for me in real project, I don't know why you think it exposes as an action, just remove default routing /:controller/(:action) from your routes. This method must be public.Polonaise
It's also working for mailers, just define the method in your subclass of ActionMailer::Base :)Jaundiced
How would you set the options based on model attribute. For example, set :profile to comment.user.city_name, so we get /newyork/comments/123?Occultation
comments_path(:profile => comment.user.city_name, :id => comment.id)Polonaise
@LukaszSliwa but how would you set the instance variable to for current_profile?Impressure
T
24

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.

Tolerate answered 23/5, 2011 at 18:12 Comment(8)
But how can i make this dependent on the current request, so for example access instance variables?Jaundiced
Yeps, it's not thread safe. I did it through custom method and Thread.currentHurtless
Looks like this is it for Rails 3.2.Supersensitive
Dumb question: where do you put this line? In routes.rb? In an initializer?Cur
If you have to set this at the config level, how do you pass in a dynamic account_id? For example scope '/:account_id' - How would you be able to set account_id dynamically at the config level?Impressure
I just put this in the Application Controller and it worked for me. Not sure if that is the right place for it, but it works.Vito
@DanTao 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.Knighthead
This no longer works for Rails 5.1.4 for page rendering, but only works for rails console.Intrigante
L
4

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

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

Linoel answered 23/8, 2012 at 19:2 Comment(1)
I think the guides are out of date, not apidoc. The method no longer works in Rails 3.2.Pi
O
0

For Rails 3 specifically, the canonical way to do it is by adding a default_url_options method to your ApplicationController.

class ApplicationController < ActionController::Base
  def default_url_options
    {
        :host => "corin.example.com",
        :port => "80"  #  Optional. Set nil to force Rails to omit
                       #    the port if for some reason it's being
                       #    included when you don't want it.
    }
  end
end

I just had to figure this out myself, so I know it works.

This is adapted from the Rails 3 Guide:
http://guides.rubyonrails.org/v3.2.21/action_controller_overview.html#default_url_options

Occasional answered 24/9, 2015 at 21:16 Comment(0)
B
0

Rails.application.routes.default_url_options[:host]= 'localhost:3000'

In the developemnt.rb / test.rb, can be more concise as following:

Rails.application.configure do
  # ... other config ...

  routes.default_url_options[:host] = 'localhost:3000'
end
Bashemath answered 16/1, 2019 at 3:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.