【发布时间】:2023-03-22 18:23:01
【问题描述】:
我在 boot.rb 中加载环境变量时遇到问题。我尝试了 2 颗宝石 dotenv 和 dotenv-rails,但没有一个对我有用。其他地方我可以使用环境变量,但不能在 boot.rb 中使用。任何人都对此有任何线索
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
【问题讨论】:
-
你没有说
RAILS_ENV的内容是什么,但是由于你的数组只有一个元素("development"),你可以将你的条件重写为if ENV['RAILS_ENV'] == "development"。它与您当前的代码相同,但更易于理解。 -
RAILS_ENV=development 在 .env 文件中设置
-
也许可以,但值是多少?另外,您确定
.env文件在执行此代码时已被处理吗?我建议打印出这个值进行调试。 -
我尝试调试它给出的 ENV['RAILS_ENV'] 的值为零。但是当我在 Rails 控制台或应用程序的任何地方检查时,它给了我正确的值。我怀疑是在启动时 .env 文件未加载
-
我已经投票结束这个问题。
标签: ruby-on-rails ruby ruby-on-rails-5.2