繁体   English   中英

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)

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

为什么 Rails 没有检测到我的自定义初始值设定项?

我正在使用 Rails 5 API only 模式。

尝试这个。

# 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM