在Rails 4.1.0上使用从ENV变量设置的config/secrets.yml变量的问题 [英] Issues using config/secrets.yml variable set from ENV variable on Rails 4.1.0
问题描述
很久以来没有与Rails玩过,所以要遍历了解Ruby on Rails 教程很棒.
Have not played with Rails in ages so walking through the Learn Ruby on Rails tutorial which is excellent.
我在使用Google身份验证时遇到问题,示例代码从下面的app/models/contact.rb模型update_spreadsheet方法调用config/secrets.yml变量(从外壳中的ENV读取)
I'm having issues with google authentication, the example code calls the config/secrets.yml variables (which are read from ENV in the shell) from the app/models/contact.rb model update_spreadsheet method below
def update_spreadsheet
connection = GoogleDrive.login(Rails.application.secrets.gmail_username, Rails.application.secrets.gmail_password)
def update_spreadsheet
connection = GoogleDrive.login(Rails.application.secrets.gmail_username, Rails.application.secrets.gmail_password)
我通过〜/.bash_profile设置了ENV变量,并已确认可以使用以下代码使一切正常,但这不是示例代码,因此我只是在进行黑客攻击.
I have the ENV variable set via my ~/.bash_profile and have confirmed using the following code I can make things work, but it's not the example code so I'm just hacking.
def update_spreadsheet
connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])
def update_spreadsheet
connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])
我可以使其正常运行,但是,我想按照教程学习,并且知道如何更好地进行故障排除.如果有人有指针,将不胜感激.
I can make it work but, I wanted to follow the tutorial and know how to troubleshoot better. if anyone has a pointer it would be appreciated.
在我的config/secrets.yml文件里面看起来像:
Inside my config/secrets.yml file looks like:
development:
gmail_username: <%= ENV["GMAIL_USERNAME"] %>
gmail_password: <%= ENV["GMAIL_PASSWORD"] %>
development:
gmail_username: <%= ENV["GMAIL_USERNAME"] %>
gmail_password: <%= ENV["GMAIL_PASSWORD"] %>
谢谢
推荐答案
Rails读取secrets.yml,但是从secrets.yml中获取值与您使用它的方式几乎没有什么不同,请检查以下代码:
Rails reads secrets.yml but getting the value from secrets.yml is little different then how you had used it, check the code below:
secrets.yml:
secrets.yml:
发展: secret_key_base:3b7cd727ee24e8444053437c36cc66c3 some_api_key:SOMEKEY
development: secret_key_base: 3b7cd727ee24e8444053437c36cc66c3 some_api_key: SOMEKEY
这是您获取值的方法:
Rails.application.secrets.some_api_key返回SOMEKEY
Rails.application.secrets.some_api_key returns SOMEKEY
这篇关于在Rails 4.1.0上使用从ENV变量设置的config/secrets.yml变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!