Set default_url_options on initialization

I need to force a host in one of the environments in my rails application.

I can make override work by turning on

def default_url_options(opts={}) opts.merge({:host => 'stg.my-host.com'}) end 

in application / controllers / application.rb

But is there a way to set this during initialization, preferably in the config / environment / ... file? I would like to leave the env conditional logic outside the controller.

But when I try

  config.action_controller.default_url_options = { ... } 

or even

 ActionController::Base.default_url_options = { ... } 

I get the undefined method, "even if the wrapper is in the config.after_initialize file {...}

any thoughts?

+4
source share
2 answers

The answer ... this is not possible because default_url_options is implemented as a function, not attr.

From action_pack / action_controller / base.rb: 1053:

  # Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in # the form of a hash, just like the one you would use for url_for directly. Example: # # def default_url_options(options) # { :project => @project.active? ? @project.url_name : "unknown" } # end # # As you can infer from the example, this is mostly useful for situations where you want to centralize dynamic decisions about the # urls as they stem from the business domain. Please note that any individual url_for call can always override the defaults set # by this method. def default_url_options(options = nil) end 
+5
source

You can force the configuration as follows:

 config.action_mailer.default_url_options = { :host => "foo.com" } 

The problem in your code is that you used config.action_controller instead of config.action_mailer

+1
source

All Articles