I had this issue when working on a Rails 6 application with Docker.
The issue was that I modified my database connection strings into environment variables in the config/database.yml
file:
default: &default
adapter: postgresql
encoding: unicode
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
host: <%= ENV['DATABASE_HOST'] %>
port: <%= ENV['DATABASE_PORT'] %>
to use it in my docker-compose.yml
file and then placed the values of the database environment variables in a .env
file in the application root directory:
DATABASE_USER=my_username
DATABASE_PASSWORD=12345678
DATABASE_NAME=my_app_development
DATABASE_HOST=localhost
DATABASE_PORT=5432
RAILS_ENV=development
RACK_ENV=development
However, Rails does not natively support reading environment variables from .env
files so the environment variable values could not be read, so it was throwing an error when I run the rails db:drop
command:
Dropped database 'my_app_development'
no implicit conversion of nil into String
Couldn't drop database ''
rails aborted!
TypeError: no implicit conversion of nil into String
Here's how I solved it:
Add the dotenv-rails
gem to your aplication Gemfile
:
gem 'dotenv-rails'
Install the dotenv-rails
gem from your console in the application root directory:
bundle install
Try running the database command that you want again. For me it was:
rails db:drop
and it should work fine this time:
Dropped database 'my_app_development'
Dropped database 'my_app_development'
That's all.
I hope this helps