在测试中尝试 运行 rails c 时获取 config.eager_load 设置为 nil

Getting config.eager_load is set to nil while trying to run rails c in test

我正在尝试 运行 rails (4.1.2) 控制台

rails c RAILS_ENV=test

我得到这个:

> config.eager_load is set to nil. Please update your
> config/environments/*.rb files accordingly:
> 
>   * development - set it to false   * test - set it to false (unless
> you use a tool that preloads your test environment)   * production -
> set it to true
> 
> /Users/xxxxxx/.rvm/gems/ruby-2.2.2/gems/activerecord-4.1.12/lib/active_record/connection_adapters/connection_specification.rb:257:in
> `resolve_symbol_connection': 'RAILS_ENV=test' database is not
> configured. Available: ["development", "test", "production"]
> (ActiveRecord::AdapterNotSpecified)

然而在我的 test.rb 中我有 config.eager_load = false 并且我的 database.yml 没问题(我 运行 rake db:schema:load RAILS_ENV=test 没有问题。

我该如何解决这个问题?

您需要在 运行 命令之前声明环境:

RAILS_ENV=test bundle exec rails c

我在我的电脑上得到了相同的输出:

> bundle exec rails c RAILS_ENV=test                                                                                                               ian@Ians-MacBook-Pro
config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:

  * development - set it to false
  * test - set it to false (unless you use a tool that preloads your test environment)
  * production - set it to true

但是当我按照建议运行时:

> RAILS_ENV=test bundle exec rails c                                                                                                               ian@Ians-MacBook-Pro
Loading test environment (Rails 4.2.3)

至于Windows个系统:

  1. 应该这样做:

    rails c -e test
    
  2. 或者先设置RAILS_ENV变量:

    set RAILS_ENV=test
    rails c
    

Windows RAILS_ENV 出现故障。如果你运行

SET RAILS_ENV=development<SPACE>   <- with a space symbol after word "development"

然后你会得到错误:

config.eager_load is set to nil. Please update your config/environments/*.rb fil es accordingly:

在做 rails server webrick.

因此请确保您输入的内容没有尾随 space(并且没有引号)

SET RAILS_ENV=development<ENTER>

这同样适用于所有环境:测试、生产和开发。

确保你有

  • production.rb

  • development.rb

  • test.rb

config/environments/ 文件夹中的文件。

需要设置

config.eager_load = false

用于 config/environments/development.rb 和 test.rb 或 = true 用于 production.rb(默认)

这很愚蠢,但是当我 运行 rails c 而不是 rails g 时,我 运行 就喜欢这个了。有道理,因为我传递给生成命令的内容对于控制台命令无效。