Environment variable in Rails and Pow Console

I cannot access the env variables in the Rails console while they work in the application.

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 , the error "ArgumentError: SMTP-AUTH requested, but there is no secret phrase" appears. However, it successfully sends mail from the application.

How can I make env variables available in the console?

+6
source share
3 answers

The Rails console cannot access the environment variable because Pow transfers the information from the .powenv or .powrc to Rails ... Rails doesn Do not read these files yourself.

In other words, you set the ENV['SENDGRID_PASSWORD'] variable ENV['SENDGRID_PASSWORD'] in the .powenv file, but this file is not affected when the Rails console starts.

You will need to configure before_filter in your Application Controller, which sets the ENV['SENDGRID_PASSWORD'] (or come up with a different, similar way to read the .powenv file from within this before_filter in your Rails).

+4
source

to try

 . .powenv 

then

 rails c 

(dot is a command to run a script in the current environment)

+11
source

For posterity, you can add something like this to your environment.rb, development.rb or initializer (config / initializers / pow.rb) depending on what loading 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
+2
source

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


All Articles