dotenv gem在Rails 6或Ruby 2.6.5中没有获取变量 [英] dotenv gem doesn't get variables in Rails 6 or Ruby 2.6.5

查看:72
本文介绍了dotenv gem在Rails 6或Ruby 2.6.5中没有获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dotenv gem 将要开发的环境变量存储在秘密文件中.升级我的计算机上的红宝石和导轨后,gem不再提取变量.为了找出原因,在尝试了不同的选择之后,我最终创建了两个相同的应用程序,但有两个区别.红宝石和铁轨版本.一个应用程序能够提取环境变量,而另一个则返回nil.有什么建议么?

I was using the dotenv gem to store environmental variables for development in a secrets file. The gem is no longer pulling the variables after upgrading ruby and rails on my computer. To try and track down the cause, and after a while of trying different options, I ended up creating two identical applications with just two differences. The ruby and the rails versions. One app is able to pull the environmental variables, the other returns nil. Any suggestions?

我的设置

正在运行的应用程序具有

Working application has

ruby "2.5.0"
gem 'rails',        '~> 5.1.6'

无法正常运行的应用具有

Non-working app has

ruby "2.6.5"
gem 'rails',        '~> 6.0.0'

到目前为止我尝试过的事情

  • 我阅读了gem网站上的信息,并尝试将 Dotenv :: Railtie.load 添加到我的 config/application.rb 文件中.
  • 此外,我尝试在我的Gemfile中添加 require:'dotenv/rails-now',以防出现其他gem问题.
  • 我在需要变量的文件中添加了 require'dotenv/load'.
  • 我在 config/application.rb 文件的各个位置添加了'require dotenv/load'
  • I read the information on the gem's site and tried adding Dotenv::Railtie.load to my config/application.rb file.
  • Also, I tried adding require: 'dotenv/rails-now' in my Gemfile in case it was because of another gem issue.
  • I added require 'dotenv/load' to the files needing the variables.
  • I added 'require dotenv/load' to various places in my config/application.rb file

我分别进行了每次更改,并使用了 byebug 来在终端中检查变量是否为已加载.而且每次变量仍为 nil 时.

Each change I did individually and used byebug to check in the terminal if the variable was loaded. And each time the variable was still nil.

我想念什么吗?有什么建议么?还是我应该切换到另一个宝石?我听说 figaro 可能会做同样的事情,只是之前从未使用过.预先感谢您的帮助.

Anything that I am missing? Any suggestions? Or should I switch to another gem? I hear that figaro may do the same thing, just never used it before. Thank you in advance for your help.

推荐答案

好,我找到了一种使其工作的方法.不知道我下面的解释是否可以解释为什么gem dotenv无法与Rails 6一起使用.但是,最新版本的Rails中现在有更好的方法.

Ok, I found a way to make it work. Not sure if my explanation below will explain why the gem dotenv doesn't work with Rails 6. But, there is a better way to do this now in the newest version of Rails.

参考

我想出了这个答案是因为Romil Mehta(

I figured out this answer because of this blog post by Romil Mehta (Rails 6 adds support for multi environment credentials)

背景

似乎从Rails 5.2开始,我们已经能够存储凭据而不是机密信息.我不知道这一点,因此继续使用上述宝石.

It seems that since Rails 5.2, we have been able to store credentials instead of secrets. I did not know this and had continued to use the above-mentioned gem.

现在会发生什么

因此,在创建新的RoR应用程序时,会在Rails应用程序中创建一个 config \ credentials.yml.enc 文件,并使用 config中的主密钥对该文件进行加密\ master.key 文件.(注意:您应该在首次提交git或可能使用的任何其他版本跟踪器之前隐藏此文件.)

So, at the creation of a new RoR app, a config\credentials.yml.enc file is created within your Rails app that is encrypted with the master key found in the config\master.key file. (NOTE: You should hide this file before your first git commit or any other version tracker you may use.)

有许多方法可以编辑加密的文件,以添加自己的变量以供开发.博客作者使用以下Rails控制台行作为示例: EDITOR = vim rails certificate:edit .我更喜欢'nano'作为编辑器,因此我将控制台行更改为: EDITOR = nano rails certificate:edit .

There are many ways to edit the encrypted file to get your own variables added for development. The blog author used this rails console line as an example: EDITOR=vim rails credentials:edit. I prefer 'nano' as my editor, so I changed the console line to: EDITOR=nano rails credentials:edit.

新方法

从nano外壳中, credentials.yml.enc 文件被解密,我可以读取它.然后,我添加了存储在我的机密文件中的凭据,这是我试图在整个应用程序中访问的凭据.像这样:

From the nano shell, the credentials.yml.enc file is decrypted and I can read it. I then added in the credentials stored in my secrets file, which I was trying to access throughout my app. Something like this:

oauth:   
   server_base_url: http://localhost:3000
   oauth_token: 123
   oauth_secret: 456

之前,在我的应用程序中,我将只通过调用 ENV ['variable_name'] 来引用其中一个密钥,就像 ENV ['server_base_url'] ENV ['oauth_token'] ,我会得到' http://localhost:3000 "或"123".现在,要执行相同的操作,我需要输入以下代码: Rails.application.credentials.section_name [:variable_name] ,其中"section_name"在上面的列表中为"oauth",其后是三个变量名.因此,要引用"oauth_token",我会做: Rails.application.credentials.oauth [:oauth_token] .

Before, in my application, I would reference one of the secret keys by just calling ENV['variable_name'] as in ENV['server_base_url'] or ENV['oauth_token'] and I would get the output of 'http://localhost:3000' or '123' respectively. Now, to do the same thing, I need to have the code: Rails.application.credentials.section_name[:variable_name], where the 'section_name' is 'oauth' in my list above, followed by three variable names. So, to reference the 'oauth_token' I would do: Rails.application.credentials.oauth[:oauth_token].

一旦我将所有 ENV 调用都更改为 Rails.application.credentials 代码,我的应用就可以使用了.它提取了秘密变量(现在为凭据"),并将我的示例应用程序连接到oauth服务器以授权登录.

Once I changed all of my ENV calls to the Rails.application.credentials code, my app worked. It pulled the secret variables ('credentials' now) and had my sample app connect to the oauth server to authorize login.

摘要

同样,我不确定这如何解释为什么gem'dotenv'在我的新Ruby和Rails环境中不起作用.但是,如果其他人也遇到相同的问题,这是您的解决方法!而且,由于它是RoR应用程序的功能,因此它可能不是解决方法,而是编码应用程序的正确方法.

Again, I am not sure how this explains why the gem 'dotenv' does not work in my new Ruby and Rails environments. But, if anyone else is having the same issue, here is a workaround for you! And as it is a feature of a RoR application, it is probably not a workaround, but the right way to code your app.

快乐编码!

这篇关于dotenv gem在Rails 6或Ruby 2.6.5中没有获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆