Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Heroku上的生产环境中使用ENV文件_Ruby On Rails_Heroku - Fatal编程技术网

Ruby on rails 在Heroku上的生产环境中使用ENV文件

Ruby on rails 在Heroku上的生产环境中使用ENV文件,ruby-on-rails,heroku,Ruby On Rails,Heroku,我的production.rb环境文件中有以下配置行,如中所示: 但当我尝试部署时,会出现一个错误: 运行:rake资产:预编译rake中止 nil的未定义方法split:NilClass /tmp/build_abdc…/config/environments/production.rb:107:inblock-in' 这是因为在编译期间配置变量不可用。有一个Heroku实验室可以用来解决这个问题,但它附带了一个来自Heroku的警告:“使用这个实验室功能被认为是违背Heroku最佳实践的。”

我的production.rb环境文件中有以下配置行,如中所示:

但当我尝试部署时,会出现一个错误:

运行:rake资产:预编译rake中止
nil的未定义方法
split:NilClass
/tmp/build_abdc…/config/environments/production.rb:107:in
block-in'

这是因为在编译期间配置变量不可用。有一个Heroku实验室可以用来解决这个问题,但它附带了一个来自Heroku的警告:“使用这个实验室功能被认为是违背Heroku最佳实践的。”


那么,在生产配置中使用环境变量的最佳实践是什么呢?它们是否都应该包装在rescue处理程序中,以便Heroku在编译过程中忽略它们?

我们最后只是在分配之前检查环境变量。无论何时在Heroku上的config/initializer中使用ENV vars,这似乎就是您需要的模式:

  # NOTE: ENV vars aren't available during slug compilation, so we must check if they exist:
  if ENV["MEMCACHEDCLOUD_SERVERS"]
    config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] }
  end
另见:

也许您可以尝试将设置移动到初始值设定项:@steakchaser-谢谢,但这并没有改变任何东西-该行在编译时仍然会被命中,并引发相同的错误muha!我喜欢资产管道!您是否尝试在预编译上设置
初始化?我不知道这在rails4中是否仍然是一件事,但它在heroku上救了我好几次。你确定你的环境中存在MEMCACHEDCLOUD_服务器吗?@phoet-有趣的文章-因此它说“在slug编译期间,你的应用程序的配置变量不在环境中,所以你应该采取措施处理配置变量为零的情况”. 然后它说关于预编译上的初始化:“在Rails 4.x中,这个选项已经被删除,不再需要”。嗯?
  # NOTE: ENV vars aren't available during slug compilation, so we must check if they exist:
  if ENV["MEMCACHEDCLOUD_SERVERS"]
    config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] }
  end