установить default_url_options при инициализации - PullRequest
4 голосов
/ 14 октября 2010

Мне нужно принудительно настроить хост в одной из сред в моем приложении rails.

Я могу заставить переопределение работать, включив

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

в приложение / контроллеры /application.rb

Но есть ли способ установить это при инициализации, желательно в файле config / environment / ...?Я хотел бы сохранить условную логику env вне контроллера.

Но когда я пытаюсь

   config.action_controller.default_url_options = { ... }

или даже

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

я получаю "неопределенный метод,"даже если завернуть в config.after_initialize {...}

есть мысли?

Ответы [ 2 ]

5 голосов
/ 14 октября 2010

Ответ ... это невозможно, потому что default_url_options реализован как функция, а не attr.

Из пакета 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
1 голос
/ 21 февраля 2011

Вы можете принудительно настроить конфигурацию следующим образом:

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

Проблема в вашем коде заключается в том, что вы использовали config.action_controller вместо config.action_mailer

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...