2014-02-09 32 views
2
在生產中使用ENV文件

我有我的production.rb環境文件下面的配置線,按照指示在this article在Heroku

config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] } 

但是,當我嘗試部署,我會得到一個錯誤:

Running: rake assets:precompile rake aborted!
undefined method split' for nil:NilClass
/tmp/build_abdc.../config/environments/production.rb:107:in
block in '

這是因爲配置變量在編譯期間不可用。有一個Heroku實驗室add-on,你可以用它來解決這個問題,但它帶有Heroku的警告:「使用這個實驗室功能被認爲與Heroku最佳實踐相反。」

那麼什麼當談到在生產配置中使用ENV變量的最佳做法?他們是否應該全部包裝在救援處理程序中,以便Heroku在編譯期間忽略它們?

+0

也許你可以嘗試設置移動到一個初始化:HTTP://計算器.com/questions/5810289/setting-the-cache-store-in-an-initializer – steakchaser

+0

@steakchaser - 謝謝但是這並沒有改變任何東西 - 該行仍然在編譯時遇到併產生相同的錯誤 – Yarin

+0

muha!我喜歡資產管道!你嘗試設置'initialize_on_precompile'嗎?我不知道這是否仍然是一個在rails4中的東西,但它在heroku上多次保存我的屁股http://stackoverflow.com/questions/11889131/precompile-failing-on-heroku-with-initialize-on-precompile-set - 假的 – phoet

回答

1

我們最終只是在分配之前檢查ENV變量。看起來像您需要的模式,只要你使用ENV在配置/初始化瓦爾在Heroku:

# NOTE: ENV vars aren't available during slug comiplation, so 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 

參見: https://devcenter.heroku.com/articles/rails-asset-pipeline#failures-in-the-assets-precompile-task