【问题标题】:Ruby - ActiveRecord::ConnectionNotEstablishedRuby - ActiveRecord::ConnectionNotEstablished
【发布时间】:2011-10-24 04:14:00
【问题描述】:

我对 Ruby 比较陌生,并且一直在关注“Ruby On Rails 3 教程 - 通过示例学习 Ruby - 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 应用程序跟踪 |框架跟踪 |全跟踪

路由和控制器 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

这是来自框架跟踪的信息:

  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

【问题讨论】:

  • 你有 config/database.yml 吗?
  • 没有。在我的 DB 文件夹中,只有一个 seed.rb 文件。我在内核中尝试了这个命令,但它出错了。耙分贝:创建分贝:迁移
  • 应该有一个默认的database.yml文件;你确定吗?
  • 它会在 db 文件夹之外的任何地方吗?
  • 是的,那个文件在那里……对不起。我仍在学习路径。无论如何,这就是文件中的内容。 # SQLite version 3.x # gem install sqlite3 # # 确保在 Gemfile 中定义了 SQLite 3 gem # gem 'sqlite3' development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 adapter: sqlite3 database: db/test.sqlite3 池:5 超时:5000 生产:适配器:sqlite3 数据库:db/production.sqlite3 池:5 超时:5000

标签: ruby-on-rails ruby


【解决方案1】:

我遇到了同样的问题,我从头开始启动示例应用程序并使用了这个 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'

这解决了我的问题:)

【讨论】:

  • 还不完全确定为什么,但这解决了问题。谢谢您的帮助!我试图弄清楚为什么上面的 gem 数据不起作用。你会知道吗?我只是不想以后再遇到这个问题。
  • sqlite 版本是主要原因.. sqlite-ruby 似乎不起作用,但在 3.1.1 中使用 sqlite3 可以工作
【解决方案2】:

确保你已经安装了 sqlite3 gem。

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

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

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

【讨论】:

  • 在哪里可以找到跟踪应用程序数据?我尝试同时运行 rake db create 和 migrate 但这样做时收到错误。我确实安装了 sqlite 以及 mysql。我在这个例子中使用 sqlite。
  • Trace 只不过是出现问题时显示的异常(错误消息)。我在上面看到您在下面提到了一些错误,即有三个链接 Application Trace |框架跟踪 | Full Trace,这些被称为trace。请发布错误,这可能会提供一些线索,说明出了什么问题...
  • 似乎是设置开发环境的问题。确保你已经安装了 sqlite3 - 你可以通过 sqlite3 --version 来检查它。还要确保你已经安装了所需的 gem,你应该有以下 gem - sqlite3 (1.3.4) 和 sqlite3-ruby (1.3.3)
  • 非常感谢您抽出宝贵时间提供帮助。谢谢。
【解决方案3】:

尝试更新 sqlite3 gem。 bundle update sqlite3

【讨论】:

  • 我会稍后再回复你。
【解决方案4】:

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

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

替换为:

宝石'sqlite3'

【讨论】:

    【解决方案5】:
    bundle install
    bundle update
    rake db:setup
    rake db:migrate
    rake db:seed
    # remember to restart the server
    rails server
    

    【讨论】:

      【解决方案6】:

      在开发环境中,运行即可

      bundle exec rake db:setup
      

      在生产环境中,运行

      bundle exec RAILS_ENV=production rake db:setup
      

      【讨论】:

        【解决方案7】:

        将此添加到您的 Gemfile:

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

        【讨论】:

          【解决方案8】:

          我是从以前版本的 Rails 迁移过来的。对我来说,解决方法是添加

          需要'rails/all'

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

          【讨论】:

            【解决方案9】:

            使用最新的 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]
            

            【讨论】:

              【解决方案10】:

              将 config/database.yml 文件中的适配器更新为您的应用程序正在使用的数据库名称并重新启动您的 Rails。这将解决问题。

              对我来说,它被重置为其他一些数据库名称,所以它给出了错误。将其更新为“sqlite3”后,它工作正常。

              【讨论】:

                【解决方案11】:

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

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

                2. 打开.config/database.yml

                  适配器:mysql2

                  数据库:specify_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] 信息 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 开始 GET "/demo/index" for 127.0.0.1

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

                  DemoController#index 作为 HTML 处理

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

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

                等等…………

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

                【讨论】:

                  【解决方案12】:

                  以下步骤对我有用..

                  1. db/ 文件夹中创建空白development.sqlite3 文件。

                  2. 关闭当前终端并创建新终端

                  然后运行rails server

                  【讨论】:

                    【解决方案13】:

                    在我的情况下,我正在尝试 wsl2 windows linux 子系统,ubuntu 16.04 我只需将确切的 gem 版本:gem 'sqlite3', '~>1.3.13' 添加到 Gemfile 中,然后再做一个 bundle install

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2012-04-05
                      • 1970-01-01
                      • 2013-06-09
                      • 1970-01-01
                      • 1970-01-01
                      • 2013-08-26
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多