2015-02-24 75 views
5

我更改了我的database.yml,以便在測試和開發中使用sqlite3數據庫,並在生產中使用postgresql。我的應用程序運行在生產正常,但當我啓動測試或開發environements我有這樣的錯誤:無法加載'rails.application.database_configuration'undefined方法'[]'爲零:NilClass

Cannot load 'Rails.application.database_configuration': 
undefined method'[]' for nil:NilClass (NoMethoError) 

我的database.yml:

# SQLite version 3.x 
# gem install sqlite3 
# 
# Ensure the SQLite 3 gem is defined in your Gemfile 
# gem 'sqlite3' 
# 
default: &default 
    adapter: sqlite3 
    pool: 5 
    timeout: 5000 

development: 
    <<: *default 
    database: db/development.sqlite3 

# 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: 
    <<: *default 
    database: db/test.sqlite3 

production: 
    pool: 5 
    timeout: 5000 
    encoding: utf8 
    adapter: postgresql 
    host: <%= Rails.application.secrets[:database][:host]%> 
    database: <%= Rails.application.secrets[:database][:name]%> 
    username: <%= Rails.application.secrets[:database][:username]%> 
    password: <%= Rails.application.secrets[:database][:password]%> 

回答

-3
host: <%= Rails.application.secrets[:database][:host]%> 
database: <%= Rails.application.secrets[:database][:name]%> 
username: <%= Rails.application.secrets[:database][:username]%> 
password: <%= Rails.application.secrets[:database][:password]%> 

主啊,不這樣做。不要這麼做,原因很多。

使用ENV瓦爾生產服務器上,而不是:

host: <%= ENV['DB_HOST'] %> 
database: <%= ENV['DB_NAME'] %> 
username: <%= ENV['DB_USER'] %> 
password: <%= ENV['DB_PASSEWORD'] %> 

然後你想要去到您的督促服務器,並在你的shell配置文件中設置這些變量(.bashrc中,.zshrc,.profile文件)如下:

export DB_HOST=your_host 
export DB_NAME=your_name 
export DB_USER=your_user 
export DB_PASSWORD=your_password 

你的問題是1 2的可能的事情:

一個。在祕密文件之前加載數據庫配置的爭用條件。

b。你有你的secrets.yml分段的環境,並沒有這些節點的開發/測試環境。

+0

感謝把我在良好的軌道,原因是缺少節點secrets.yml – scauglog 2015-02-25 15:46:45

+2

所以..它歸結爲'添加'database'節'development'和'test'環境secrets.yml'。對? – 2016-03-21 16:12:10

+0

我使用'ENV'變量數據庫連接,我已經把我的裏面設置'secrets.yml' ..但是運行時'軌s'並嘗試訪問Rails應用程序我得到這個錯誤'拒絕訪問用戶'root'@'localhost'(使用密碼:否)'..任何想法? – Lykos 2016-10-20 19:58:35

1

除了關於安全

如果你打算走這條路,這是很好的,你需要確保的一個非常重要的事情:

你secrets.yml文件被安全地提供,不簽入源代碼控制,而不是由任何人誰不應該有 訪問

在這種情況下,它比其鎖定的*key/*crt證書文件F0毫不遜色安全可讀你的網絡服務器,除了你可能存儲多個祕密。這樣做正確比祕密ENV更好,但仍然有更好的選擇。

一些進一步閱讀:

您的實際問題

你的問題是@ tagCincy的第二個想法(「B 「),奇怪的是,這很容易解決。只要做@ Nik-Olai建議的。聲明(儘管空白)相同參數的值在你的開發/測試環境是這樣的:

development: 
    ... 
    database: 
    :name: 
    :username: 
    :password: 

test: 
    # same thing here 

的原因是,雖然production數據庫配置是不能爲developmenttest環境中使用,解析該配置的仍然會發生。

把不同的方式,甚至在測試/開發模式生產數據庫的設置仍然會得到解析(和ERB-解釋)。

Rails.application.secrets是依賴於環境的,所以在開發和測試模式下Rails.application.secrets[:database]不會存在,這意味着後續的[]調用會打到零的目標,瞧!

1

就遇上了這樣的新的Rails 5.1.0在與加密文件中的新祕密加入我的變量。

你的nil正在發生,因爲即使它正在開發中,它仍然會嘗試加載所有內容,並且你從nil - > something [:nil] [:cant_grab_from_nil]調用了一些東西。快速的解決方法是if語句。只有在新Rails 5.1中使用祕密加密文件時才建議使用。

production: 
    pool: 5 
    timeout: 5000 
    encoding: utf8 
    adapter: postgresql 
    <% if Rails.application.secrets[:database].present? %> 
    host: <%= Rails.application.secrets[:database][:host]%> 
    database: <%= Rails.application.secrets[:database][:name]%> 
    username: <%= Rails.application.secrets[:database][:username]%> 
    password: <%= Rails.application.secrets[:database][:password]%> 
    <% end %> 
相關問題