Ruby – ActiveRecord :: ConnectionNotEstablished
我对Ruby比较陌生,并且一直关注着“Ruby On Rails 3教程 – 通过示例学习Ruby – Michael Hartl”一书。 我目前正在讨论静态页面的第3章。
在本章中,我将下面的命令input到提示符中: rails generate controller Pages home contact
和一切工作正常。
然后这本书指引我到http://localhost:3000/pages/home
。 当我指向我的浏览器时,我收到以下错误。
的ActiveRecord :: ConnectionNotEstablished 的ActiveRecord :: ConnectionNotEstablished Rails.root:/ home / ralph / railsprojects / sample_app 应用程序跟踪| 框架跟踪| 完整跟踪
路由和控制器erb似乎没有任何错误。 这是一个数据库相关的错误? 有任何想法吗?
谢谢,DMAT
更新:
这是我的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
以下是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
我有同样的问题,我从头开始的示例应用程序,并使用这个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'
这解决了我的问题:)
确保你已经安装了sqlite3的gem。
如果是这样,你运行下面的命令,
rake db:create # to create database rake db:migrate # to create tables based on your migration
如果上述两个工作正常,您的应用程序应该能够连接到数据库。 否则请在这里复制跟踪应用程序跟踪,这可能会帮助我们更好地帮助您。
尝试更新sqlite3的gem。 bundle update sqlite3
如果你在你的Gemfile里有这样的东西:
gem'sqlite3-ruby','> 1.2.0',:require =>'sqlite3'
将其replace为:
gem'sqlite3'
bundle install bundle update rake db:setup rake db:migrate rake db:seed # remember to restart the server rails server
在开发环境中,运行
bundle exec rake db:setup
在生产环境中运行
bundle exec RAILS_ENV=production rake db:setup
将此添加到您的Gemfile中:
gem 'sqlite3-ruby', :require => 'sqlite3'
我正在从以前的Rails版本迁移。 对于我来说,补充是补充
要求'rails / all'
在application.rb的顶部,而不是5个单独的要求,我有。
使用最新的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]
将config / database.yml文件中的适配器更新为您的应用程序正在使用的数据库名称,然后重新启动您的导轨。 这将解决这个问题。
对我来说,它重置为其他的数据库名称,所以这是给错误的。 更新到“sqlite3”后,它工作正常。
我是新来的,但这可能是非常有帮助的(如果你正在开发只是检查发展假生产和testing环境):
-
创build一个数据库,并给它一个名字。
-
打开
.config/database.yml
适配器:mysql2
database:specify_your_newly_created_database #check是否自动更新
游泳池:5
用户名:您指定的用户名。
密码:您指定的密码。
主机:localhost
-
$ rails server
=>引导WEBrick
=> Rails 3.2.9应用程序从http:// 0.0.0.0:3000开始开发
=>用-d呼叫分离
=> Ctrl-Cclosures服务器
[2012-12-12 23:31:50] INFO WEBrick 1.3.1
[2012-12-12 23:31:50] INFOruby1.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:32:40 +0545开始GET“/ demo / index”为127.0.0.1
连接到由database.yml指定的数据库
将DemoController#index处理为HTML
渲染布局/应用程序中的demo / index.html.erb(0.0ms)
编译demo.css(46ms)(pid 4068)
等等………..
如果你只是试图解决ActiceRecord,那么这将解决问题,并显示你的第一个问候页面。 这是一些时候发生的问题,因为在处理第一个hello页面时,rails会search数据库,尽pipe我们和数据库没有任何关系,所以只要检查一次,如果不是,我们会一起解决。 干杯!