3

I have a file called frontend_configuration.rb which looks like this:

class FrontEndConfiguration
  class << self
    attr_accessor :base_url
  end
end

In my development config, I have a line:

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

When I try to run my rails server, it gives me an error saying:

uninitialized constant FrontEndConfiguration (NameError)

I am following this Stackoverflow answer here: Rails 3 / Setting Custom Environment Variables

Any ideas why Rails isn't detecting my custom initializer?

I am using Rails 5 API only mode.

Community
  • 1
  • 1
Zhang
  • 11,549
  • 7
  • 57
  • 87

1 Answers1

5

Try this.

# 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

And now,

Rails.configuration.frontend['base_url'] 
# => production_base_url or development_base_url
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Tony Vincent
  • 13,354
  • 7
  • 49
  • 68
  • Is this the recommended way of doing it in Rails 5 now? It certainly works for what I want to achieve. – Zhang Dec 15 '16 at 05:38
  • @Zhang yes it is.See the [documentation](http://guides.rubyonrails.org/configuring.html#custom-configuration) – Tony Vincent Dec 15 '16 at 05:47