Ruby - ActiveRecord::ConnectionNotEstablished

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7871233/
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-03 02:14:09  来源:igfitidea点击:

Ruby - ActiveRecord::ConnectionNotEstablished

ruby-on-railsruby

提问by Dmat00

I am rather new to Ruby and have been following along with the book "Ruby On Rails 3 Tutorial - Learn Ruby by Example - by Michael Hartl". I am currently in Chapter 3 which discusses static pages.

我是 Ruby 的新手,一直在关注“Ruby On Rails 3 教程 - 通过示例学习 Ruby - Michael Hartl”一书。我目前在讨论静态页面的第 3 章。

In this chapter, I entered the following command into the prompt: rails generate controller Pages home contactand everything worked fine.

在本章中,我在提示符中输入了以下命令:rails generate controller Pages home contact一切正常。

Then the book directs me to http://localhost:3000/pages/home. When I point my browser there, I receive the following error.

然后这本书将我引导到http://localhost:3000/pages/home. 当我将浏览器指向那里时,我收到以下错误。

ActiveRecord::ConnectionNotEstablished

ActiveRecord::ConnectionNotEstablished
Rails.root: /home/ralph/railsprojects/sample_app

Application Trace | Framework Trace | Full Trace

The routes and controller erb don't appear to have any errors. Is this a database related error? Any ideas?

路由和控制器 erb 似乎没有任何错误。这是与数据库相关的错误吗?有任何想法吗?

Thanks, DMAT

谢谢,DMAT

Update:

更新:

This is the code in my database.yml file.

这是我的 database.yml 文件中的代码。

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# 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:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

Here is the information from the Framework Trace:

以下是来自框架跟踪的信息:

  activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in `retrieve_connection'
  activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
  activerecord (3.1.1)lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
  activerecord (3.1.1) lib/active_record/query_cache.rb:65:in `call'
  activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `call'
  activesupport (3.1.1) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
  activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `send'
  activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:68:in `call'
  rack (1.3.5) lib/rack/sendfile.rb:101:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
  railties (3.1.1) lib/rails/rack/logger.rb:13:in `call'
  rack (1.3.5) lib/rack/methodoverride.rb:24:in `call'
  rack (1.3.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.3.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call'
  railties (3.1.1) lib/rails/engine.rb:456:in `call'
  railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call'
  railties (3.1.1) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.3.5) lib/rack/handler/webrick.rb:59:in `service'
  /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  /usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
  /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
  /usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
  rack (1.3.5) lib/rack/handler/webrick.rb:13:in `run'
  rack (1.3.5) lib/rack/server.rb:265:in `start'
  railties (3.1.1) lib/rails/commands/server.rb:70:in `start'
  railties (3.1.1) lib/rails/commands.rb:54
  railties (3.1.1) lib/rails/commands.rb:49:in `tap'
  railties (3.1.1) lib/rails/commands.rb:49
  script/rails:6:in `require'
  script/rails:6

采纳答案by buzzer123

I had the same problem, I started the sample-app from scratch and used this gemfile:

我遇到了同样的问题,我从头开始启动了示例应用程序并使用了这个 gemfile:

source 'http://rubygems.org'

gem 'rails', '3.1.1'

gem 'sqlite3'

group :development do
  gem 'rspec-rails', '2.6.1'
end


group :test do
  gem 'rspec-rails', '2.6.1'
  gem 'webrat', '0.7.1'
end

group :assets do
  gem 'sass-rails',   '~> 3.1.4'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

This solved the problem for me :)

这为我解决了问题:)

回答by nkm

Make sure that you have installed sqlite3 gem.

确保你已经安装了 sqlite3 gem。

If so did you run the following commands,

如果是这样,您是否运行了以下命令,

rake db:create # to create database
rake db:migrate # to create tables based on your migration

If the above two works fine, your application should be able to connect to the database. Else please copy the trace application trace over here, that may help us to help you better.

如果上述两个工作正常,您的应用程序应该能够连接到数据库。否则,请将跟踪应用程序跟踪复制到这里,这可能有助于我们更好地帮助您。

回答by Artem Kalinchuk

Try updating the sqlite3 gem. bundle update sqlite3

尝试更新 sqlite3 gem。 bundle update sqlite3

回答by freytag

If you have in your Gemfile something like:

如果您的 Gemfile 中有类似的内容:

gem 'sqlite3-ruby', '> 1.2.0', :require => 'sqlite3'

gem 'sqlite3-ruby', '> 1.2.0', :require => 'sqlite3'

replace it with:

替换为:

gem 'sqlite3'

宝石'sqlite3'

回答by nikc

bundle install
bundle update
rake db:setup
rake db:migrate
rake db:seed
# remember to restart the server
rails server

回答by hao

In development environment, just run

在开发环境中,只需运行

bundle exec rake db:setup

In production environment, run

在生产环境中,运行

bundle exec RAILS_ENV=production rake db:setup

回答by ashim888

i am new to it but this might be pretty helpful(if you are in development just check development leave production and test environments):

我是新手,但这可能会很有帮助(如果您正在开发中,只需检查开发离开生产和测试环境):

  1. Create a database and give it a name.

  2. Open up .config/database.yml

    adapter: mysql2

    database: specify_your_newly_created_database #check if its automatically updated or not

    pool: 5

    username: your designated username.

    password: your designated password.

    host: localhost

  3. $ rails server

    => Booting WEBrick

    => Rails 3.2.9 application starting in development on http:// 0. 0. 0 .0 :3000

    => Call with -d to detach

    => Ctrl-C to shutdown server

    [2012-12-12 23:31:50] INFO WEBrick 1.3.1

    [2012-12-12 23:31:50] INFO ruby 1.9.3 (2012-11-10) [i386-mingw32]

    [2012-12-12 23:31:50] INFO WEBrick::HTTPServer#start: pid=4068 port=3000

    Started GET "/demo/index" for 127.0.0.1 at 2012-12-12 23:32:40 +0545

    Connecting to database specified by database.yml

    Processing by DemoController#index as HTML

    Rendered demo/index.html.erb within layouts/application (0.0ms)

    Compiled demo.css (46ms) (pid 4068)

  1. 创建一个数据库并为其命名。

  2. 打开 .config/database.yml

    适配器:mysql2

    数据库:指定_your_newly_created_database #检查是否自动更新

    池:5

    用户名:您指定的用户名。

    密码:您指定的密码。

    主机:本地主机

  3. $ rails server

    => 启动 WEBrick

    => Rails 3.2.9 应用程序在 http:// 0. 0. 0 .0 :3000 上开始开发

    => 用 -d 调用来分离

    => Ctrl-C 关闭服务器

    [2012-12-12 23:31:50] 信息 WEBrick 1.3.1

    [2012-12-12 23:31:50] 信息红宝石 1.9.3 (2012-11-10) [i386-mingw32]

    [2012-12-12 23:31:50] 信息 WEBrick::HTTPServer#start: pid=4068 port=3000

    在 2012-12-12 23:32:40 +0545 开始为 127.0.0.1 获取“/demo/index”

    连接到由 database.yml 指定的数据库

    由 DemoController#index 处理为 HTML

    在布局/应用程序中呈现 demo/index.html.erb (0.0ms)

    编译demo.css (46ms) (pid 4068)

and so on...........

等等...........

if your'e just trying to solve the ActiceRecord then this will solve the problem and display your first hello page. This is the problem that occurs sometime since rails searches for the database though we have nothing to do with database when dealing with the first hello page, so just check it once if not the we will solve it together. Cheers!

如果您只是想解决 ActiceRecord,那么这将解决问题并显示您的第一个 hello 页面。这是Rails搜索数据库时出现的问题,虽然我们在处理第一个hello页面时与数据库无关,所以检查一次,如果没有我们一起解决。干杯!

回答by Sachin Maharjan

Add this to your Gemfile:

将此添加到您的 Gemfile 中:

gem 'sqlite3-ruby', :require => 'sqlite3'

回答by Adam Loving

I was migrating from a previous version of Rails. For me the fix was to add

我是从以前版本的 Rails 迁移的。对我来说,修复是添加

require 'rails/all'

需要 'rails/all'

at the top of application.rb instead of the 5 separate requires I had.

在 application.rb 的顶部,而不是我所拥有的 5 个单独的要求。

回答by Matthew Saeed

Using the latest sqlite3 + bundle update + bundle install worked for me. I believe the restriction to an older sqlite3 version per the walkthrough is likely messing things up. Here's my gemfile:

使用最新的 sqlite3 + bundle update + bundle install 对我有用。我相信每个演练对旧的 sqlite3 版本的限制可能会搞砸。这是我的 gemfile:

source 'https://rubygems.org'

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



# Use sqlite3 as the database for Active Record
gem 'sqlite3'


# 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

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

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

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

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