dotenv gem doesn't get variables in Rails 6 or Ruby 2.6.5

六眼飞鱼酱① 提交于 2021-01-21 05:20:51

问题


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?

My Setup

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'

What I have tried so far

  • 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

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.

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.


回答1:


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.

Reference

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

Background

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.

What Happens Now

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

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.

The New Way

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

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

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.

Summary

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.

Happy Coding!




回答2:


I had a similar problem where dotenv was not loading environment variables after adding it to the Gemfile and running bundle. Spring needs to be restarted for the changes to take effect. After spring stop anad restarting the rails console/server the ENV variables were recognized.



来源:https://stackoverflow.com/questions/58737414/dotenv-gem-doesnt-get-variables-in-rails-6-or-ruby-2-6-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!