簡體   English   中英

Ruby - ActiveRecord :: ConnectionNotEstablished

[英]Ruby - ActiveRecord::ConnectionNotEstablished

我是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

我有同樣的問題,我從頭開始使用示例應用程序並使用此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

如果以上兩個工作正常,您的應用程序應該能夠連接到數據庫。 否則請在此處復制跟蹤應用程序跟蹤,這可以幫助我們更好地幫助您。

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

嘗試更新sqlite3 gem。 bundle update sqlite3

如果你的Gemfile中有這樣的東西:

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

替換為:

寶石'sqlite3'

在開發環境中,只需運行即可

bundle exec rake db:setup

在生產環境中,運行

bundle exec RAILS_ENV=production rake db:setup

我是新手,但這可能非常有用(如果您正在開發中,請檢查開發離開生產和測試環境):

  1. 創建一個數據庫並為其命名。

  2. 打開.config/database.yml

    適配器:mysql2

    database:specify_your_newly_created_database #check如果它自動更新或不自動更新

    游泳池:5

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

    密碼:您指定的密碼。

    主持人:localhost

  3. $ 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

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

    編譯demo.css(46ms)(pid 4068)

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

如果您只是想解決ActiceRecord,那么這將解決問題並顯示您的第一個問候頁面。 這是因為rails在搜索數據庫時有時會發生這個問題,雖然在處理第一個hello頁面時我們與數據庫沒有任何關系,所以如果不是我們將一起解決它,只需檢查一次。 干杯!

將其添加到您的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文件中的適配器更新為應用程序使用的數據庫名稱,然后重新啟動rails。 這將解決問題。

對我來說,它被重置為其他一些數據庫名稱,因此它給出了錯誤。 將其更新為“sqlite3”后,它運行正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM