Rails/ActiveRecord - AdapterNotSpecified,即使它是

Nek*_*oru 8 ruby activerecord ruby-on-rails

我正在做Ruby on Rails教程.对于前三章,它使用了SQLite,但后来它建议在开发时使用PostgreSQL来简化Heroku的部署.在编辑我database.ymlGemfile使用pg而不是sqlite3后,它似乎有效 - 除非使用Rake运行测试.它弹出一个AdapterNotSpecified错误.

C:/Ruby/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/ connection_adapters/connection_specification.rb:52:in resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecif ied)

等等

database.yml指定了一个适配器,如下所示:

development:
  adapter: postgresql
  host: localhost
  username: nekkoru
  password: derpderp
  database: development
  encoding: UTF8
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?我使用的是Windows 7 x64,Ruby 1.9.3,Rails 4.0.0,PostgreSQL 9.3.0.1.

spi*_*ann 14

您没有为测试环境定义数据库.您的database.yml文件应如下所示:

development:
  adapter: postgresql
  host: localhost
  username: nekkoru
  password: derpderp
  database: development
  encoding: UTF8

test:
  adapter: postgresql
  host: localhost
  username: nekkoru
  password: derpderp
  database: test        # or whatever the name is
  encoding: UTF8
Run Code Online (Sandbox Code Playgroud)


Red*_*Red 6

缩进是我的罪魁祸首.

Rails似乎有一种严格的方式从yml文件中读取数据库类型(开发或生产)和数据库设置(适配器,主机,用户名等).

但当然ActiveRecord错误消息太过分了......它让我觉得我错过了一个宝石

  • 谢谢.你真的救了我的命. (2认同)