Uninitialized initializer constant

2019-07-31 15:56发布

问题:

I created a settings.rb file under the initializers folder containing values I need initialized once the application starts. However, on running rails s I get a "Uninitialized contant Settings(NameError)

Settings.rb

Settings.defaults[:single_phase] = 500
Settings.defaults[:three_phase]  = 300

I created the migration to accompany it already and the view.

Where is the problem?

回答1:

In your config/application_settings.rb

development:
  single_phase: 200

and use it anywhere in your app

APP_SETTINGS['single_phase']

which returns 200



回答2:

At the time initializers run they do not have access to the model (is it a model?).

If it isn't a model, you can do the following:

SETTINGS = {}
SETTINGS[:single_phase] = 500

However I feel like the figaro gem might be helpful for what you are trying to do.

You can also add arbitrary settings in the application.rb and environment files.