Ruby-on-rails 无法访问“database.yml”文件中的环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8686215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failing to access environment variables within `database.yml` file
提问by jefflunt
I have the following developement section of my development.ymlfile:
我的文件有以下开发部分development.yml:
development:
adapter: postgresql
host: localhost
database: testtb
username: app_user
password: ENV['APP_USER_POSTGRES_PASSWORD'] <= Troublesome line
When I open a rails console via bundle exec rails consoleand type ENV['APP_USER_POSTGRES_PASSWORD']I get back the DB password I've specified in my local profile. However, when I start my rails server, it can't connect to the DB, failing with
当我通过打开 rails 控制台bundle exec rails console并键入时,ENV['APP_USER_POSTGRES_PASSWORD']我会取回我在本地配置文件中指定的数据库密码。但是,当我启动 Rails 服务器时,它无法连接到数据库,失败
PGError FATAL: password authentication failed for user "app_user"
This was previously working when I had the DB password actually typed out in plain text, rather than trying to access it via ENV['...'], but for obvious reasons I want to keep the actual password out of this file entirely (and therefore out of the code repository) while still being able to commit other, non-secure changes to the database.ymlfile.
当我实际以纯文本形式输入数据库密码而不是尝试通过 访问它时,这以前是有效的ENV['...'],但出于明显的原因,我想将实际密码完全保留在该文件之外(因此在代码存储库之外)同时仍然能够提交对database.yml文件的其他非安全更改。
Is there something special about the syntax I'm missing, or are the environment variables for some reason not available when the database.ymlfile is being loaded?
我缺少的语法有什么特别之处,或者database.yml在加载文件时由于某种原因环境变量不可用?
回答by jefflunt
Update:Some people report in the comments that this doesn't work as of Rails 4.2.x.x. I haven't tried it myself, so YMMV.
更新:有些人在评论中报告说这在 Rails 4.2.xx 中不起作用我自己没有尝试过,所以 YMMV。
Ah, finally figured out the simple solution - it accepts embedded Ruby:
啊,终于找到了简单的解决方案 - 它接受嵌入式 Ruby:
password: <%= ENV['APP_USER_POSTGRES_PASSWORD'] %>
回答by FireDragon
Short and quick solution if you are running a Rails version > 4.2 Run the following command:
如果您运行的是 Rails 版本 > 4.2 的简短且快速的解决方案,请运行以下命令:
spring stop
..then run rails consoleor other rails command. My issue was that Spring server needed to be restarted in order to refresh/pickup my new ENV vars. I was starting up Rails console and it couldn't see them until I shut down Spring.
..然后运行rails console或其他 rails 命令。我的问题是需要重新启动 Spring 服务器才能刷新/提取我的新 ENV 变量。我正在启动 Rails 控制台,但在我关闭 Spring 之前它看不到它们。
Previous versions of Rails didn't have this issue since they didn't use Spring server.
以前版本的 Rails 没有这个问题,因为他们没有使用 Spring 服务器。
Another tool to help you troubleshoot -- Use the following command to print out your database.yml config. You can run it from the command line, but I prefer to run this within Rails console since then you can use awesome_print to make it pretty:
另一个帮助您排除故障的工具——使用以下命令打印您的 database.yml 配置。你可以从命令行运行它,但我更喜欢在 Rails 控制台中运行它,从那时起你可以使用 awesome_print 使它漂亮:
Within rails console:
内rails console:
puts ActiveRecord::Base.configurations
...or using awesome_print
...或使用 awesome_print
ap ActiveRecord::Base.configurations
Or instead from the command line:
或者从命令行改为:
bin/rails runner 'puts ActiveRecord::Base.configurations'

