我对Ruby很陌生,并且一直关注着“Ruby On Rails 3 Tutorial - Learn Ruby by Example - Michael Hartl”一书。我目前在第3章讨论静态页面。
在本章中,我在提示符中输入了以下命令:rails generate controller Pages home contact
,一切正常。
然后这本书指引我http://localhost:3000/pages/home
。当我将浏览器指向那里时,我收到以下错误。
ActiveRecord::ConnectionNotEstablished ActiveRecord::ConnectionNotEstablished Rails.root: /home/ralph/railsprojects/sample_app Application Trace | Framework Trace | Full Trace
路由和控制器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
答案 0 :(得分:19)
我遇到了同样的问题,我从头开始使用示例应用程序并使用了这个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'
这解决了我的问题:)
答案 1 :(得分:10)
确保已安装sqlite3 gem。
如果是,您运行以下命令,
rake db:create # to create database
rake db:migrate # to create tables based on your migration
如果以上两个工作正常,您的应用程序应该能够连接到数据库。否则,请在此处复制跟踪应用程序跟踪,这可以帮助我们更好地帮助您。
答案 2 :(得分:2)
尝试更新sqlite3 gem。 bundle update sqlite3
答案 3 :(得分:2)
如果您的Gemfile中包含以下内容:
gem'sqlite3-ruby','> 1.2.0',:require => 'sqlite3的'
将其替换为:
gem'sqlite3'
答案 4 :(得分:2)
bundle install
bundle update
rake db:setup
rake db:migrate
rake db:seed
# remember to restart the server
rails server
答案 5 :(得分:1)
在开发环境中,只需运行
bundle exec rake db:setup
在生产环境中,运行
bundle exec RAILS_ENV=production rake db:setup
答案 6 :(得分:0)
我是新手,但这可能非常有用(如果您正在开发中,只需检查开发离开生产和测试环境):
创建数据库并为其命名。
打开.config/database.yml
adapter:mysql2
数据库:指定_your_newly_created_database #check是否自动更新
pool:5
用户名:您指定的用户名。
密码:您指定的密码。
host:localhost
$ rails server
=>启动WEBrick
=> Rails 3.2.9应用程序在http:// 0. 0. 0 .0:3000
开始开发=>使用-d调用分离
=> Ctrl-C关闭服务器
[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
在2012-12-12 23:32:40 +0545开始获取127.0.0.1的GET“/ demo / index”
连接到database.yml指定的数据库
DemoController处理#index为HTML
在layouts / application(0.0ms)中呈现demo / index.html.erb
编译demo.css(46ms)(pid 4068)
依旧...........
如果您只是想解决ActiceRecord,那么这将解决问题并显示您的第一个问候页面。这是因为rails在搜索数据库时有时会发生这个问题,虽然在处理第一个hello页面时我们与数据库没有任何关系,所以如果不是我们将一起解决它,只需检查一次。干杯!
答案 7 :(得分:0)
将此添加到您的Gemfile:
gem 'sqlite3-ruby', :require => 'sqlite3'
答案 8 :(得分:0)
我从以前版本的Rails迁移。对我来说,修复是添加
要求'rails / all'
在application.rb的顶部,而不是我有的5个单独的要求。
答案 9 :(得分:0)
使用最新的sqlite3 + bundle update + bundle install为我工作。我相信每次演练对旧的sqlite3版本的限制可能会搞砸了。这是我的宝石文件:
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]
答案 10 :(得分:0)
将config / database.yml文件中的适配器更新为应用程序使用的数据库名称,然后重新启动rails。这将解决问题。
对我来说,它被重置为其他一些数据库名称,所以它给出了错误。将其更新为“sqlite3”后,效果很好。
答案 11 :(得分:0)
以下步骤对我有用。.
在development.sqlite3
文件夹中创建空白db/
文件。
关闭当前终端并创建新终端
然后运行rails server
答案 12 :(得分:0)
在我的情况下,我正在尝试 wsl2 windows linux 子系统,ubuntu 16.04 我只需要添加确切的 gem 版本:gem 'sqlite3', '~>1.3.13'
到 Gemfile 并执行另一个 bundle install