rails config / environments在哪里定义“ config”?

时间:2018-09-18 12:58:07

标签: ruby-on-rails ruby

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 个答案:

答案 0 :(得分:2)

这些类包括ActiveSupport::Configurable模块:

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

这里是您自己实现此行为的替代方法:

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