Rails 5 custom initializer not working

2.1k views Asked by At

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.

1

There are 1 answers

2
Tony Vincent On BEST ANSWER

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