Ismoh
Ismoh

Reputation: 1104

Ruby on Rails 4.0.4: How do I set up the Environment Variables?

How do I set up the environment variables?
I already tried it as you can see.

Code-Snippet:

app/local_env.yml:

MAIL_DOMAIN: 'gmail.com'
MAIL_USERNAME: "[email protected]"
MAIL_PASSWORD: "password"


app/config/application.rb:

config.before_configuration do
  env_file = File.join(Rails.root, 'config', 'local_env.yml')
  YAML.load(File.open(env_file)).each do |key, value|
    ENV[key.to_s] = value
  end if File.exists?(env_file)
end


output (RailsConsole):

>> ENV['MAIL_USERNAME']
nil

>> ENV['MAIL_DOMAIN']
nil

Can anyone help me fix it?

Upvotes: 2

Views: 1173

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

You're trying to load your file from the config folder using File.join(Rails.root, 'config', 'local_env.yml'), but you saved the file in the root of your app.

Your YAML.load block only executes if the file exists; since it can't find config/local_env.yml, it doesn't load anything.

You should move local_env.yml to your config folder. If you'd rather keep the file in the root of your app, then change the line to:

env_file = File.join(Rails.root, 'local_env.yml')

Upvotes: 3

Related Questions