Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

ruby-on-rails - 在Ruby on Rails应用程序中加载.env(Loading .env in Ruby on Rails application)

I am setting up a heroku and I am using using this guide to set up my local environment for development.

(我正在设置一个heroku,并且正在使用本指南来设置我的本地开发环境。)

I have a '.env' file in the root of my application with the variable set up as noted in the guide.

(我在应用程序的根目录中有一个“ .env”文件,并按照指南中的说明进行了设置。)

When I run

(当我跑步)

heroku local:run rails runner "puts ENV['S3_BUCKET']"

it returns a blank.

(它返回一个空白。)

How do I get it to recognize/load the '.env' file?

(我如何获得它来识别/加载“ .env”文件?)

  ask by npresco translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I recommend using https://github.com/bkeepers/dotenv gem if you want to load .env file with ENV variables.

(如果您要使用ENV变量加载.env文件,建议使用https://github.com/bkeepers/dotenv gem。)

If you are already using it then according to dotenv readme you should load as soon as possible on boot, for example in config/application.rb add:

(如果您已经在使用它,那么根据dotenv自述文件,您应该在引导时尽快加载,例如在config / application.rb中添加:)

require 'dotenv'
Dotenv.load

If you use PUMA or Unicorn, in config/puma.rb, config/unicorn.rb add:

(如果使用PUMA或Unicorn,请在config / puma.rb中的config / unicorn.rb中添加:)

  require "dotenv"
  Dotenv.load("/path/to/env/.env")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...