Uninitialized initializer constant

2019-07-31 15:52发布

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?

2条回答
Ridiculous、
2楼-- · 2019-07-31 16:42

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-07-31 16:56

In your config/application_settings.rb

development:
  single_phase: 200

and use it anywhere in your app

APP_SETTINGS['single_phase']

which returns 200

查看更多
登录 后发表回答