ruby ActiveRecord::AdapterNotSpecified 数据库配置未指定适配器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23336755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:27:37  来源:igfitidea点击:

ActiveRecord::AdapterNotSpecified database configuration does not specify adapter

rubypostgresqlactiverecordherokuruby-on-rails-4

提问by user3408293

When I use heroku open my web app works fine but when I'm using rails s (localhost) I am running into this error:

当我使用 heroku 打开我的网络应用程序工作正常但是当我使用 rails s (localhost) 我遇到了这个错误:

ActiveRecord::AdapterNotSpecified database configuration does not specify adapter

Why is this?

为什么是这样?

This is my database.yml

这是我的database.yml

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

And this is my gemfile:

这是我的 gemfile:

source 'https://rubygems.org'


gem 'pg'

gem 'bootstrap-sass', '~> 3.1.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

采纳答案by Uri Agassi

For you app to work locally you need to:

要使您的应用程序在本地工作,您需要:

  1. Install Postgresqlon your machine
  2. Create a databasefor your development needs (let's call it my_app_development)
  3. Change your database.ymlto:

    default: &default
      adapter: postgresql
      encoding: unicode
      # For details on connection pooling, see rails configuration guide
      # http://guides.rubyonrails.org/configuring.html#database-pooling
      pool: 5
    
    development:
      <<: *default
      database: my_app_development
    
  4. run rake db:migrate

  1. 在你的机器上安装 Postgresql
  2. 为您的开发需求创建一个数据库(我们称之为my_app_development
  3. 将您更改database.yml为:

    default: &default
      adapter: postgresql
      encoding: unicode
      # For details on connection pooling, see rails configuration guide
      # http://guides.rubyonrails.org/configuring.html#database-pooling
      pool: 5
    
    development:
      <<: *default
      database: my_app_development
    
  4. rake db:migrate

回答by yekta

You didn't show the command causing this query, but this could happen if you pass a string and not a symbol.

您没有显示导致此查询的命令,但如果您传递的是字符串而不是符号,则可能会发生这种情况。

For example:

例如:

irb(main):001:0> ActiveRecord::Base.establish_connection("#{Rails.env}")
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter

But then if you use a symbol, it will work.

但是如果你使用一个符号,它就会起作用。

irb(main):001:0> ActiveRecord::Base.establish_connection("#{Rails.env}".to_sym)
=> #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007f2f484a32a0 #....

回答by SreekanthGS

Your database.yml should look something like this:

您的 database.yml 应如下所示:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5
  username: my_username
  password: my_password

development:
  <<: *default
  database: "development_database_name"

test:
  <<: *default
  database: "test_database_name"

production:
  <<: *default
  database: "production_database_name"

Edit development_database_name to your local database name. Also edit my_username and my_password to your correct db username and password.

将 development_database_name 编辑为您的本地数据库名称。还要将 my_username 和 my_password 编辑为正确的数据库用户名和密码。

回答by JAGJ jdfoxito

Delete tabs nothing more, ident perfect, such as:

删除标签罢了,ident完美,如:

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: utf8
  pool: 5
  host: 192.168.0.121
  username: postgres
  password: passpostgres

development:
  <<: *default
  database: DBPOSTGRES
 # Warning: The database defined as "test" will be erased and
 # re-generated from your development database when you run "rake".
 # Do not set this db to the same as development or production.
test:
  <<: *default
  database: DBPOSTGRES
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: DBPOSTGRES
  password: <%= ENV['passpostgres'] %>

回答by lacostenycoder

In case you're trying to use activerecord without rails you may run into this problem with a database.yml with multiple environment setups. So you'll need to pass the environment key into the config setup like this:

如果您尝试在没有 Rails 的情况下使用 activerecord,您可能会遇到具有多个环境设置的 database.yml 的问题。因此,您需要像这样将环境密钥传递到配置设置中:

DB_ENV ||= 'development'
connection_details = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(connection_details[DB_ENV])

回答by Richard Peck

Why are you using a yml node referencein your database.yml?

你为什么要使用YML节点参考你的database.yml

You should have something like this:

你应该有这样的事情:

#config/database.yml
development:
  adapter: mysql2
  encoding: utf8
  database: ****
  pool: 5
  username: ****
  password: ****
  host: ***.***.***.*** #-> only for third party db server

production:
  adapter: postgresql
  encoding: utf8
  database: ****
  pool: 5
  username: ****
  password: ****
  host: ***.***.***.*** #-> only for third party db server


Update

更新

Rails runs using a database. You haveto connect to a db to make it work, and to do that you have to define the different connection details in database.yml

Rails 使用数据库运行。您必须连接到数据库才能使其工作,为此您必须在database.yml

To define the right information, you need to appreciate that Rails operates in several environments- development& productionbeing the two most used

要定义正确的信息,你需要明白,Rails的几个工作environments-developmentproduction被两个最常用的

To get Rails working in your local (development) environment, you need to define the correct db details. This means you need a database to connect to - which is typically done setting up a local mysql / pgsql server

要让 Rails 在您的本地(开发)环境中工作,您需要定义正确的数据库详细信息。这意味着您需要一个要连接的数据库 - 这通常是通过设置本地 mysql / pgsql 服务器完成的

Bottom line is you connect to a db using:

底线是您使用以下方法连接到数据库:

  • hostname
  • username
  • password
  • db name
  • 主机名
  • 用户名
  • 密码
  • 数据库名称

You need to define these in your config/database.ymlfile

您需要在config/database.yml文件中定义这些

If you have a server running in your local environment, your database.yml file will look like this:

如果您在本地环境中运行服务器,您的 database.yml 文件将如下所示:

#config/database.yml
development:
  adapter: mysql2
  encoding: utf8
  database: db_name
  pool: 5
  username: username
  password: password