4

When I do the following:

rake db:drop
rake db:create
rake db:migrate
rake db:seed

This is what I get:

rake aborted!
no implicit conversion of nil into string    
app/mailers/base_mailer.rb:47:in '[]'
app/mailers/base_mailer.rb:47:in 'chimp_mail'

I think this has to do with mailchimp variables I have in my .env file. How would I pass these variables directly into bash?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
uralika
  • 193
  • 2
  • 8

3 Answers3

3

ENV

You can only use ENV variables if you have them available in your environment (OS). They are, after all, called environment variables:

Operating systems (Linux, Mac OS X, Windows) provide mechanisms to set local environment variables, as does Heroku and other deployment platforms. Here we show how to set local environment variables in the Unix shell. We also show two alternatives to set environment variables in your application without the Unix shell

I think your problem will likely be that you've not got your environment variables set up in such a way that your Rails app can access them in the backend


Figaro

I would try using Figaro

This is a gem which allows you to create & define any number of ENV variables, and have them accessible in your development environment. I would set the env variables in the application.yml file that's created with Figaro and try to run the db:seed command again

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
2

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

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
1

The crucial piece to the puzzle that I should have mentioned is that I am using foreman. Therefore, the solution to my problem was running "foreman rake db:reset".

uralika
  • 193
  • 2
  • 8