Cannot load 'rails.application.database_configuration' undefined method '[]' for nil: NilClass

I modified my database.yml to use the sqlite3 database in tests and development, and postgresql in production. My application works fine during the production process, but when I run the testing or development tags, I get this error:

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

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]%> 
+5
source share
3 answers
 host: <%= Rails.application.secrets[:database][:host]%> database: <%= Rails.application.secrets[:database][:name]%> username: <%= Rails.application.secrets[:database][:username]%> password: <%= Rails.application.secrets[:database][:password]%> 

Lord, do not do this. Do not do this for many reasons.

Instead, use ENV vars on the production server:

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

Then you want to go to your prod server and set these variables in the shell configuration 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 

Your problem is 1 out of 2 possible things:

a. A race condition when database configurations are uploaded to a secrets file.

b. You have secrets.yml secrets that are not environments and do not have these nodes for a development / testing environment.

-3
source

Beyond Security

If you are going to follow this route, this is normal, you need to make sure of the importance:

Your secrets.yml file is well prepared, not verified in the source control and cannot be read by anyone who should not have access

In this case, it is no less secure than a file with a private *key/*crt file for your web server, except that you potentially keep several secrets. The correct implementation of this question is preferable to place secrets in ENV , but there are even better options.

Further reading:

Your actual problem

Your problem is @tagCincy's second idea ("b"), and, oddly enough, this is very easy to fix. Just do what Nick Olai suggested. Declare (albeit empty) the values โ€‹โ€‹of the same parameters in your test environments, like this:

 development: ... database: :name: :username: :password: test: # same thing here 

The reason is that although the configuration of the production database is not used in development or test environments, parsing this configuration will still occur.

In another way, the settings of your production database will still be parsed (and interpreted by ERB) even in test / dev mode.

Rails.application.secrets depends on the environment, so in dev and test modes Rails.application.secrets[:database] will not exist, which means that the next call [] will hit the target nil and voila!

+1
source

Include this with the new Rails 5.1.0, adding my variables to a new secret file.

Your zero happens because, although it is a development, it still tries to load everything and you call things from nil โ†’ something [: nil] [: cant_grab_from_nil]. A quick workaround is the if statement. Only suggest this when using a secret file in the new 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 %> 
+1
source

Source: https://habr.com/ru/post/1214082/


All Articles