Rails 5 自定义初始化器不工作

Rails 5 custom initializer not working

我有一个名为 frontend_configuration.rb 的文件,它看起来像这样:

class FrontEndConfiguration
  class << self
    attr_accessor :base_url
  end
end

在我的 development 配置中,我有一行:

FrontEndConfiguration.base_url = "http://localhost:4200"

当我尝试 运行 我的 rails 服务器时,它给我一个错误提示:

uninitialized constant FrontEndConfiguration (NameError)

我在这里关注这个 Whosebug 答案:Rails 3 / Setting Custom Environment Variables

知道为什么 Rails 没有检测到我的自定义初始化程序吗?

我正在使用 Rails 5 API 模式。

试试这个。

# config/frontend.yml:
production:
  environment: production
  base_url: http://your_url
  .
  .
  .
development:
  environment: sandbox
  base_url: http://your_url
  .
  .
  .
# config/application.rb
module MyApp
  class Application < Rails::Application
    config.frontend = config_for(:frontend)
  end
end

而现在,

Rails.configuration.frontend['base_url'] 
# => production_base_url or development_base_url