ruby-on-rails - 在初始化时设置 default_url_options

标签 ruby-on-rails initialization boot actioncontroller

我需要在我的 rails 应用程序的环境之一中强制主机。

我可以通过包括

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

在 app/controllers/application.rb 中

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

但是当我尝试
   config.action_controller.default_url_options = { ... }

甚至
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

关于ruby-on-rails - 在初始化时设置 default_url_options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3928622/

相关文章:

linux - 启动时在 ramdisk 上挂载/var - Bash 脚本问题

android - 启动后立即恢复小部件状态

python - 在 python 脚本中使用 IPMI (OpenIPMI)

ruby-on-rails - 获取 XML 文件 POST 请求以使用 Ruby on Rails 解析

ruby-on-rails - Ruby 1.9.1 真的准备好并更快地部署新的 Rails 了吗?

c++ - 让编译器初始化数组或手动遍历数组进行初始化更快吗?

reference - 为什么要在 go 中分配对结构的引用?

ruby-on-rails - 为什么 "new"方法保存我的关系而不是仅仅初始化它

ruby-on-rails - Rails 验证 : Scoped Uniqueness on a Boolean

javascript - 在react-native功能组件中哪里实现初始化代码