无法为 nil:NilClass 加载“rails.application.database_configuration"未定义的方法“[]" [英] cannot load 'rails.application.database_configuration' undefined method'[]' for nil:NilClass
问题描述
我将 database.yml 更改为在测试和开发中使用 sqlite3 数据库,在生产中使用 postgresql.我的应用程序在生产环境中运行良好,但是当我启动测试或开发环境时出现此错误:
I changed my database.yml to use sqlite3 database in test and developement and postgresql in production. My application run fine in production but when I launch test or development environements i have this error :
Cannot load 'Rails.application.database_configuration':
undefined method'[]' for nil:NilClass (NoMethoError)
我的database.yml:
my 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]%>
推荐答案
host: <%= Rails.application.secrets[:database][:host]%>
database: <%= Rails.application.secrets[:database][:name]%>
username: <%= Rails.application.secrets[:database][:username]%>
password: <%= Rails.application.secrets[:database][:password]%>
哦,上帝,不要这样做.出于多种原因不要这样做.
Oh Lord, don't do this. Don't do this for many reasons.
在生产服务器上使用 ENV
变量:
Use ENV
vars on the production server instead:
host: <%= ENV['DB_HOST'] %>
database: <%= ENV['DB_NAME'] %>
username: <%= ENV['DB_USER'] %>
password: <%= ENV['DB_PASSEWORD'] %>
然后你想进入你的 prod 服务器并在你的 shell 配置文件(.bashrc、.zshrc、.profile)中设置这些变量,如下所示:
Then you want to go onto your prod server and set those variables in your shell config file (.bashrc, .zshrc, .profile) as follows:
export DB_HOST=your_host
export DB_NAME=your_name
export DB_USER=your_user
export DB_PASSWORD=your_password
您的问题是 2 个可能的问题之一:
Your issue is 1 of 2 possible things:
一个.在机密文件之前加载数据库配置的竞争条件.
a. A race condition where the database configs are being loaded prior to the secrets file.
B.您将您的 secrets.yml 分段为环境,并且没有用于开发/测试环境的那些节点.
b. You have your secrets.yml segmented be environments and do not have those nodes for development/test environments.
这篇关于无法为 nil:NilClass 加载“rails.application.database_configuration"未定义的方法“[]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!