3

当我执行以下操作时:

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

这就是我得到的:

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'

我认为这与我的 .env 文件中的 mailchimp 变量有关。我如何将这些变量直接传递给 bash?

4

3 回答 3

2

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

于 2014-03-14T13:03:49.640 回答
1

我应该提到的难题的关键部分是我正在使用工头。因此,我的问题的解决方案是运行“foreman rake db:reset”。

于 2014-03-15T19:00:00.840 回答
1

我在使用Docker处理Rails 6 应用程序时遇到了这个问题。

问题是我将数据库连接字符串修改为config/database.yml文件中的环境变量:

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'] %>

在我的docker-compose.yml文件中使用它,然后将数据库环境变量的值放在.env应用程序根目录的文件中:

DATABASE_USER=my_username
DATABASE_PASSWORD=12345678
DATABASE_NAME=my_app_development
DATABASE_HOST=localhost
DATABASE_PORT=5432

RAILS_ENV=development
RACK_ENV=development

但是,Rails本身不支持从文件中读取环境变量,因此无法读取环境变量值,因此在我运行命令.env时会引发错误:rails db:drop

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

这是我解决它的方法

dotenv-railsgem 添加到您的应用程序中Gemfile

gem 'dotenv-rails'

dotenv-rails在应用程序根目录中从控制台安装gem:

bundle install

尝试再次运行所需的数据库命令。对我来说是:

rails db:drop

这次应该可以正常工作:

Dropped database 'my_app_development'
Dropped database 'my_app_development'

就这样。

我希望这有帮助

于 2020-08-07T06:45:01.947 回答