初始化时设置default_url_options

发布于 2024-09-27 16:36:14 字数 559 浏览 1 评论 0原文

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

来使覆盖起作用

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

我可以通过包含在 app/controllers/application.rb 中

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

但是当我尝试

   config.action_controller.default_url_options = { ... }

甚至

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

得到“未定义的方法”时,即使包装在 config.after_initialize { ... } 中,

有什么想法吗?

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

I've can get the override to work by including

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

in app/controllers/application.rb

But is there a way to set this on initialize, preferably in a config/environments/... file? I'd like to keep conditional env logic out of the controller.

But when I try

   config.action_controller.default_url_options = { ... }

or even

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

I get "undefined method," even if a wrap in a config.after_initialize { ... }

any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-10-04 16:36:14

答案是......这是不可能的,因为 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

The answer is...it's impossible because default_url_options is implemented as a function, not an 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文