0

背景客運忽略RAILS_ENV

我有一個ENV文件與各種變量通過我的Rails應用程序依賴於,包括RAILS_ENV,這是在這個文件中初始化的發展。我也有database.yml文件爲我的環境定義數據庫連接(見下文)。當我運行rails控制檯時,一切看起來都應該如此。檢查Rails.configuration.database_configuration[Rails.env]返回如下:

{"adapter"=>"postgresql", 
"encoding"=>"utf8", 
"database"=>"dev", 
"username"=>"rails", 
"password"=>"***", 
"host"=>"localhost", 
"pool"=>5, 
"timeout"=>5000} 

然而,當我嘗試訪問的應用程序,我得到一個502,並記錄以下錯誤:

異常的ActiveRecord :: NoDatabaseError機架中的應用對象( FATAL:數據庫「督促」不存在

顯然,該錯誤信息是準確的,但並不代表我會想到會發生

我假設這是一個Passenger/Apache問題,因爲運行rails控制檯一切都很開心。

注意:我已經看到這個在其他帖子中出現,所以沒有 - 沒有DATABASE_URL環境變量浮動在從database.yml重寫的東西周圍。

我已經試過

  1. .bashrcPassenger is supposed to source the bashrc for the user apache is running as,這又是設置到源相關ENV文件。
  2. /etc/sysconfig/httpd:我已經試過手動直接從Apache配置採購文件和傾倒ENV腳本運行正確的值使它成爲了ENV,但是這也不會改變正常的行爲時,文件驗證。

的database.yml

development: 
    adapter: postgresql 
    encoding: utf8 
    database: dev 
    username: rails 
    password: <%= ENV['RAILS_DB_PWD'] %> 
    host: <%= ENV['RAILS_DB_HOST'] %> 
    pool: 5 
    timeout: 5000 

# Warning: The database defined as "test" will be erased and 
# re-generated from your development database when you run "rake". 
# Do not set this db to the same as development or production. 
test: 
    adapter: postgresql 
    encoding: utf8 
    database: test 
    username: rails 
    password: <%= ENV['RAILS_DB_PWD'] %> 
    host: <%= ENV['RAILS_DB_HOST'] %> 
    pool: 5 
    timeout: 5000 

production: 
    adapter: postgresql 
    encoding: utf8 
    database: prod 
    username: rails 
    password: <%= ENV['RAILS_DB_PWD'] %> 
    host: <%= ENV['RAILS_DB_HOST'] %> 
    port: 5432 
    pool: 5 
    timeout: 5000 

回答

0

一種更好的方式做,這是通過簡單地使用DATABASE_URL的環境變量。

如果你有兩個配置/ database.yml中和ENV['DATABASE_URL']設置,那麼 Rails會合並在一起的配置。
Rails Guides: Configuring a Database

common: &common 
    adapter: postgresql 
    encoding: utf8 
    template: template0 # Required for UTF8 encoding 
    pool: 5 
    timeout: 5000 

development: 
    <<: *common 
    database: dev 

test: 
    <<: *common 
    database: test 

production: 
    <<: *common 
    database: prod 

我一般會建議您避免在database.yml指定數據庫的用戶名和密碼。使用ENV變量盧克!儘管你已經到了一半,但最好還是堅持一個慣例而不是配置方法,而不是引入單獨的變量。

一個應用程序是否正確分解出來的代碼 所有配置的試金石是代碼庫是否可以作出任何 時刻開源,不損害任何憑據。 https://12factor.net/config

+0

呵呵。那就是訣竅。沒有意識到您在公共塊中使用的符號。 – Dan