Environment variable in Rails console and Pow
Asked Answered
C

3

6

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?

Candiot answered 10/5, 2012 at 4:6 Comment(3)
Have you tried explicitly setting it in the console? SENDGRID_PASSWORD=123 && UserMailer.welcome_mail.deliverMason
No, it doesn't work this way too.Candiot
err I meant: SENDGRID_PASSWORD=123 rails consoleMason
I
4

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).

Idell answered 13/6, 2012 at 15:0 Comment(4)
"(or come up with another, similar, way of reading in the .powenv file from within that before_filter in your Rails app)" Yep, that would be fabulous... :) Any ideas?Adur
I don't know if you'd want to actually read the .powenv file from your Rails app, since there could be other stuff in there that you don't want/need to reload each time.Idell
If your .powenv file is super clean & each line is just setting environmental variables, then you might be able to read it in line by line within the before_filter. The before_filter could execute each line it reads out of the file. ... I'm pretty sure there are all sorts of wrong things going on with this idea, though ...Idell
Yes, it's easy to read in .powenv line by line and only execute those lines which set env vars. Unfortunately those will not be available in the console (or in Rake) that way...Adur
M
11

try

. .powenv

then

rails c

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

Merrimerriam answered 25/1, 2013 at 16:3 Comment(0)
I
4

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).

Idell answered 13/6, 2012 at 15:0 Comment(4)
"(or come up with another, similar, way of reading in the .powenv file from within that before_filter in your Rails app)" Yep, that would be fabulous... :) Any ideas?Adur
I don't know if you'd want to actually read the .powenv file from your Rails app, since there could be other stuff in there that you don't want/need to reload each time.Idell
If your .powenv file is super clean & each line is just setting environmental variables, then you might be able to read it in line by line within the before_filter. The before_filter could execute each line it reads out of the file. ... I'm pretty sure there are all sorts of wrong things going on with this idea, though ...Idell
Yes, it's easy to read in .powenv line by line and only execute those lines which set env vars. Unfortunately those will not be available in the console (or in Rake) that way...Adur
G
2

For posterity, you can add something like this to either your environment.rb, development.rb, or an initializer (config/initializers/pow.rb) depending on what load order you want:

# Load pow environment variables into development and test environments
if File.exist?(".powenv")
  IO.foreach('.powenv') do |line|
    next if !line.include?('export') || line.blank?
    key, value = line.gsub('export','').split('=',2)
    ENV[key.strip] = value.delete('"\'').strip
  end
end
Goebbels answered 22/1, 2014 at 6:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.