Trt Trt
Trt Trt

Reputation: 5552

Rails 4 Initializer Not Loading

I am trying to put my emailer password in a .yml file.

Under config/initializers I have a file emailers_config.rb

require 'yaml'
EMAIL_CONFIG = YAML.load(File.read(Rails.root + "config/mailer_config.yml"))

and in my config/mailer_config.yml I have:

#production password
smtp_password_pro: foo
#devevopment env password
smtp_password_dev: bar

Now it seems My initializes is not running, because I get this uninitialized constant EMAIL_CONFIG (NameError)

Now Rails is supposed to laod everything under the initializers folder, so loading the file is not an issue.

What is wrong here?

Upvotes: 6

Views: 3838

Answers (4)

akshaymani
akshaymani

Reputation: 623

Try something like this: ENGINE_CONFIG = YAML.load(File.read(File.join(Rails.root, "config/subfolder", "engine.yml")))[Rails.env]

Upvotes: 0

sealocal
sealocal

Reputation: 12507

If you are having trouble accessing a constant in your YAML file, try shutting down your local server with control + c, then run:

$ spring stop

Boot up your server or console again:

$ rails [server | console]

And you might have access to that constant.

Upvotes: 3

Roman Kiselenko
Roman Kiselenko

Reputation: 44380

I do not know the answer to your question but I can recommend another method
Passwords can be stored easier to .env file https://i.sstatic.net/jbcAO.png
like this

#Root dir create file ".env"
PASSWORD=123456

and load password

#Somewhere in app
ENV['PASSWORD'] #=> 123456

it works I hope will help you

enter image description here

Upvotes: 1

user2503775
user2503775

Reputation: 4367

Try this:

In your config/mailer_config.yml:

  development:
    smtp_password: foo
  production:
    smtp_password: bar

And then, in emailers_config.rb:

  EMAIL_CONFIG = YAML.load_file("#{Rails.root}/config/mailer_config.yml")[Rails.env]

Now, you will get your password for every env, using:

  EMAIL_CONFIG['smtp_password']

Upvotes: 0

Related Questions