[英]Load environment variable in boot file
I am facing issue while loading envrionment variable in boot.rb.我在 boot.rb 中加载环境变量时遇到问题。 I tried with 2 gems dotenv & dotenv-rails but none of them worked for me.
我尝试了 2 个宝石 dotenv 和 dotenv-rails,但没有一个对我有用。 Other places I am able to use environment variable but not in boot.rb.
其他地方我可以使用环境变量,但不能在 boot.rb 中使用。 Anyone have any clue on this one
任何人都对这个有任何线索
if ["development"].include?(ENV['RAILS_ENV'])
require 'bootsnap/setup'
Bootsnap.setup(
cache_dir: 'tmp/cache', # Path to your cache
development_mode: 'development', # Current working environment, e.g. RACK_ENV, RAILS_ENV, etc
load_path_cache: true, # Optimize the LOAD_PATH with a cache
autoload_paths_cache: true, # Optimize ActiveSupport autoloads with cache
compile_cache_iseq: true, # Compile Ruby code into ISeq cache, breaks coverage reporting.
compile_cache_yaml: true # Compile YAML into a cache
)
end
DotEnv will only load your .env
file into env once the Gem has been loaded. DotEnv 只会在 Gem 加载后将您的
.env
文件加载到 env 中。
In Rails this is done by the line Bundler.require(*Rails.groups)
in config/application.rb
.在 Rails 中,这是由
config/application.rb
Bundler.require(*Rails.groups)
行完成的。 If you need to access the env vars before that you need to manually call Dotenv::Railtie.load
:如果您需要在此之前访问环境变量,则需要手动调用
Dotenv::Railtie.load
:
Bundler.require(*Rails.groups)
Dotenv::Railtie.load
Or at least thats what the readme would lead you to belive .或者至少这就是自述文件会让您相信的内容。 Instead of the Railtie I guess you could use the plain ruby approach:
我想你可以使用普通的 ruby 方法而不是 Railtie:
require 'dotenv'
Dotenv.load
But there are tons options to set ENV vars from a file such as direnv which is not a Ruby gem and hooks into your shell itself.但是有很多选项可以从一个文件中设置 ENV 变量,比如direnv ,它不是一个 Ruby gem 并且钩到你的 shell 本身。
在测试环境变量之前,您可以使用 - 根据您的需要 - load
或require
显式处理您的.env
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.