【问题标题】:set default_url_options on initialize在初始化时设置 default_url_options
【发布时间】:2010-10-13 22:40:00
【问题描述】:

我需要在我的 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 { ... }

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails initialization boot actioncontroller


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-12-15
      • 1970-01-01
      • 2012-11-05
      • 2019-09-02
      • 2018-03-09
      • 2022-07-23
      • 1970-01-01
      • 2021-09-29
      • 2019-08-24
      相关资源
      最近更新 更多