Rails 4 Initializer Does Not Load

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

In config/initializers , I have an emailers_config.rb file

 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 that my initializations are not working, because I get this uninitialized constant EMAIL_CONFIG (NameError)

Now Rails should bark everything in the initializers folder, so downloading the file is not a problem.

What is wrong here?

+6
source share
4 answers

I do not know the answer to your question, but I can recommend another method
Passwords are easier to store in an .env file http://i.stack.imgur.com/jbcAO.png
like this

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

and password to download

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

it works i hope helps you

enter image description here

+1
source

If you have problems accessing the constant in your YAML file, try disabling the local server using control + c , and then run:

 $ spring stop 

Download your server or console again:

$ rails [server | console]

And you can have access to this constant.

+4
source

Try the following:

In 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 each env using:

  EMAIL_CONFIG['smtp_password'] 
0
source

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

0
source

Source: https://habr.com/ru/post/959118/


All Articles