代码之家  ›  专栏  ›  技术社区  ›  xxjjnn

Rails配置/定义了“config”的环境?

  •  4
  • xxjjnn  · 技术社区  · 7 年前

    config/environments/production.rb (以及其他环境)有:

    config.eager_load = true
    

    还有一大堆其他的 config.foobar 电话。但是“config”是从哪里来的?通常情况下,您会遇到如下情况:

    SomeClass.each do | block_variable |
      block_variable.some_method
    end
    

    但在神秘的“config”中,情况并非如此,它就像一个块变量,在任何地方都不会声明。打开Rails控制台时:

    irb(main):001:0> config
    NameError: undefined local variable or method `config' for main:Object
    Did you mean?  conf
            from (irb):1
    irb(main):002:0>
    

    同样的结果也发生在 app.config

    irb(main):002:0> app.config
    NoMethodError: undefined method `config' for #<ActionDispatch::Integration::Session:0x00007fc898d02808>
            from (irb):2
    

    Ruby如何允许调用“config”?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Rodrigo    7 年前

    这些课程包括 ActiveSupport::Configurable 模块:

    https://api.rubyonrails.org/classes/ActiveSupport/Configurable.html

    以下是您自己实现此行为的替代方法:

    https://robots.thoughtbot.com/mygem-configure-block