Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variable in Rails console and Pow

I can't access env variables in the Rails console, while in the application they work.

In .powenv I have export SENDGRID_PASSWORD="123"

In config/initializers/mail.rb there is:

ActionMailer::Base.smtp_settings = {
  :password => ENV['SENDGRID_PASSWORD']
}

So in the console when I type UserMailer.welcome_mail.deliver there is an error 'ArgumentError: SMTP-AUTH requested but missing secret phrase'. However from the application it sends the mail successfully.

How can I make the env variables available in the console?

like image 213
Martin Petrov Avatar asked May 10 '12 04:05

Martin Petrov


People also ask

How do I see environment variables in rails?

Use command ENV in rails console. That will return a hash of your environmental values you can access. Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted.

How do I pass an environment variable in Ruby?

To pass environment variables to Ruby, simply set that environment variable in the shell. This varies slightly between operating systems, but the concepts remain the same. To set an environment variable on the Windows command prompt, use the set command.

What is $_ ENV?

$_ENV is another superglobal associative array in PHP. It stores environment variables available to current script. $HTTP_ENV_VARS also contains the same information, but is not a superglobal, and now been deprecated. Environment variables are imported into global namespace.


2 Answers

try

. .powenv

then

rails c

(dot is a command to run script on current environment)

like image 65
kuboon Avatar answered Sep 21 '22 10:09

kuboon


Your Rails console isn't able to access the environment variable because Pow passes information from the .powenv or .powrc file into Rails ... Rails doesn't read those files on its own.

In other words, you're setting the ENV['SENDGRID_PASSWORD'] variable in the .powenv file, but that file is not being touched when you start the Rails console.

You'll need to set up a before_filter in your Application Controller that sets the ENV['SENDGRID_PASSWORD'] (or come up with another, similar, way of reading in the .powenv file from within that before_filter in your Rails app).

like image 42
James Chevalier Avatar answered Sep 21 '22 10:09

James Chevalier