本文介绍了Rails 控制台和 Pow 中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 Rails 控制台中访问 env 变量,而在应用程序中它们可以工作.

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

.powenv 我有 export SENDGRID_PASSWORD="123"

config/initializers/mail.rb 中有:

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

因此,当我在控制台中键入 UserMailer.welcome_mail.deliver 时,会出现错误ArgumentError:请求 SMTP-AUTH 但缺少密码短语".但是从应用程序它成功发送邮件.

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.

如何使 env 变量在控制台中可用?

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

推荐答案

您的 Rails 控制台无法访问环境变量,因为 Pow 传递信息来自 .powenv.powrc 文件 进入 Rails ... Rails 不会自行读取这些文件.

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.

换句话说,您在 .powenv 文件中设置了 ENV['SENDGRID_PASSWORD'] 变量,但是当您启动Rails 控制台.

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.

您需要在应用程序控制器中设置一个 before_filter 来设置 ENV['SENDGRID_PASSWORD'](或者想出另一种类似的方式来设置ENV['SENDGRID_PASSWORD']从 Rails 应用程序中的 before_filter 中读取 .powenv 文件).

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

这篇关于Rails 控制台和 Pow 中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 13:36