简体   繁体   English

Ruby - ActiveRecord :: ConnectionNotEstablished

[英]Ruby - ActiveRecord::ConnectionNotEstablished

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". 我是Ruby的新手,并且一直关注着“Ruby On Rails 3 Tutorial - Learn Ruby by Example - Michael Hartl”一书。 I am currently in Chapter 3 which discusses static pages. 我目前在第3章讨论静态页面。

In this chapter, I entered the following command into the prompt: rails generate controller Pages home contact and everything worked fine. 在本章中,我在提示符中输入以下命令: rails generate controller Pages home contact ,一切正常。

Then the book directs me to http://localhost:3000/pages/home . 然后这本书将我引导到http://localhost:3000/pages/home When I point my browser there, I receive the following error. 当我将浏览器指向那里时,我收到以下错误。

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. 路由和控制器erb似乎没有任何错误。 Is this a database related error? 这是数据库相关的错误吗? Any ideas? 有任何想法吗?

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: 以下是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

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 :) 这解决了我的问题:)

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. 否则请在此处复制跟踪应用程序跟踪,这可以帮助我们更好地帮助您。

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

Try updating the sqlite3 gem. 尝试更新sqlite3 gem。 bundle update sqlite3

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'

In development environment, just run 在开发环境中,只需运行即可

bundle exec rake db:setup

In production environment, run 在生产环境中,运行

bundle exec RAILS_ENV=production rake db:setup

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 打开.config/database.yml

    adapter: mysql2 适配器:mysql2

    database: specify_your_newly_created_database #check if its automatically updated or not database:specify_your_newly_created_database #check如果它自动更新或不自动更新

    pool: 5 游泳池:5

    username: your designated username. 用户名:您指定的用户名。

    password: your designated password. 密码:您指定的密码。

    host: localhost 主持人:localhost

  3. $ rails server

    => Booting WEBrick =>启动WEBrick

    => Rails 3.2.9 application starting in development on http:// 0. 0. 0 .0 :3000 => Rails 3.2.9应用程序在http:// 0. 0. 0 .0:3000开始开发

    => Call with -d to detach =>使用-d调用以分离

    => Ctrl-C to shutdown server => Ctrl-C关闭服务器

    [2012-12-12 23:31:50] INFO WEBrick 1.3.1 [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 ruby​​ 1.9.3(2012-11-10)[i386-mingw32]

    [2012-12-12 23:31:50] INFO WEBrick::HTTPServer#start: pid=4068 port=3000 [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 在2012-12-12 23:32:40 +0545开始获取127.0.0.1的GET“/ demo / index”

    Connecting to database specified by database.yml 连接到database.yml指定的数据库

    Processing by DemoController#index as HTML 由DemoController处理#index为HTML

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

    Compiled demo.css (46ms) (pid 4068) 编译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. 如果您只是想解决ActiceRecord,那么这将解决问题并显示您的第一个问候页面。 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. 这是因为rails在搜索数据库时有时会发生这个问题,虽然在处理第一个hello页面时我们与数据库没有任何关系,所以如果不是我们将一起解决它,只需检查一次。 Cheers! 干杯!

将其添加到您的Gemfile:

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

I was migrating from a previous version of Rails. 我从以前版本的Rails迁移。 For me the fix was to add 对我来说,修复是添加

require 'rails/all' 要求'rails / all'

at the top of application.rb instead of the 5 separate requires I had. 在application.rb的顶部,而不是我有的5个单独的要求。

Using the latest sqlite3 + bundle update + bundle install worked for me. 使用最新的sqlite3 + bundle update + bundle install为我工作。 I believe the restriction to an older sqlite3 version per the walkthrough is likely messing things up. 我相信每次演练对旧的sqlite3版本的限制可能会搞砸了。 Here's my gemfile: 这是我的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]

Update the adapter in config/database.yml file to the database name being used by your application and restart your rails. 将config / database.yml文件中的适配器更新为应用程序使用的数据库名称,然后重新启动rails。 This will resolve the issue. 这将解决问题。

For me it got reset to some other DB name, so it was giving error. 对我来说,它被重置为其他一些数据库名称,因此它给出了错误。 After updating it to "sqlite3", it worked fine. 将其更新为“sqlite3”后,它运行正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM