可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
When I use heroku open my web app works fine but when I'm using rails s (localhost) I am running into this error:
ActiveRecord::AdapterNotSpecified database configuration does not specify adapter
Why is this?
This is my 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:
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]
回答1:
For you app to work locally you need to:
- Install Postgresql on your machine
- Create a database for your development needs (let's call it
my_app_development
) Change your database.yml
to:
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:
run rake db:migrate
回答2:
Your database.yml should look something like this:
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:
Edit development_database_name to your local database name. Also edit my_username and my_password to your correct db username and password.
回答3:
Delete tabs nothing more, ident perfect, such as:
# 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: # production:
回答4:
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) => #<:connectionadapters::connectionpool:0x007f2f484a32a0>
回答5:
Why are you using a yml node reference in your 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 have to connect to a db to make it work, and to do that you have to define the different connection details in database.yml
To define the right information, you need to appreciate that Rails operates in several environments
- development
& production
being the two most used
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
Bottom line is you connect to a db using:
- hostname
- username
- password
- db name
You need to define these in your config/database.yml
file
If you have a server running in your local environment, your database.yml file will look like this:
#config/database.yml development: adapter: mysql2 encoding: utf8 database: db_name pool: 5 username: username password: password