初始化时设置default_url_options

2024-01-07

我需要强制主机进入我的 Rails 应用程序中的环境之一。

我可以通过包含来使覆盖起作用

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

在应用程序/控制器/application.rb

但是有没有办法在初始化时设置它,最好是在 config/environments/... 文件中?我想将条件环境逻辑保留在控制器之外。

但当我尝试时

   config.action_controller.default_url_options = { ... }

or even

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

即使在 config.after_initialize { ... } 中进行换行,我也会得到“未定义的方法”

有什么想法吗?


答案是......这是不可能的,因为 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

初始化时设置default_url_options 的相关文章

随机推荐