【问题标题】:Connection Not Enstablished - Ruby on Rails未建立连接 - Ruby on Rails
【发布时间】:2016-02-23 11:35:50
【问题描述】:

我是 Ruby on Rails 的新手,我正在试一试。 我提前说我没有找到适合我的问题的答案,也没有在 StackOverflow 和互联网上找到答案(我尝试了从 SO 和其他网站挑选的许多不同的东西)。

我在 CodeSchool 上关注 Rails for Zombies 2,我需要设置环境以使事情发生。

为了做到这一点,我正在遵循 Michael Hartl 的设置阶段 Ruby on Rails 教程。

我遵循了每一步,但是,当我想在浏览器中看到:“hello, world!”时,我得到:

ActiveRecord::ConnectionNotEstablished

No connection pool for ActiveRecord::Base

完整跟踪:

activerecord (4.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:566:in `retrieve_connection'
activerecord (4.2.2) lib/active_record/connection_handling.rb:113:in `retrieve_connection'
activerecord (4.2.2) lib/active_record/connection_handling.rb:87:in `connection'
activerecord (4.2.2) lib/active_record/query_cache.rb:51:in `restore_query_cache_settings'
activerecord (4.2.2) lib/active_record/query_cache.rb:43:in `rescue in call'
activerecord (4.2.2) lib/active_record/query_cache.rb:31:in `call'
activerecord (4.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.2) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.2) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.2) lib/active_support/callbacks.rb:776:in `_run_call_callbacks'
activesupport (4.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.2) lib/rails/engine.rb:518:in `call'
railties (4.2.2) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'

在得到这个之前,我安装了 Rails,我进入了我的 hello_app 文件夹并使用了:Bundle Install,然后我只使用了:Rails Server。我还按照指南中的说明删除了 index.html。

我的 Gemfile:

source 'https://rubygems.org'

gem 'rails',                '4.2.2'
gem 'sass-rails',           '5.0.2'
gem 'uglifier',             '2.5.3'
gem 'coffee-rails',         '4.1.0'
gem 'jquery-rails',         '>= 3.0.0'
gem 'turbolinks',           '2.3.0'
gem 'jbuilder',             '2.2.3'
gem 'sdoc',                 '0.4.0', group: :doc

group :development, :test do
  gem 'sqlite3'
  gem 'pry-byebug', platform: [:ruby_20]
  gem 'web-console', '2.0.0.beta3'
  gem 'spring',      '1.1.3'
end

我的应用程序控制器:

class ApplicationController < ActionController::Base
      protect_from_forgery with: :exception

      def hello
        render text: "hello, world!"
      end
end

我的路线:

HelloApp::Application.routes.draw do
        root 'application#hello
end

我的数据库:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

如果我的问题是微不足道的,我很抱歉,我也为我糟糕的英语感到抱歉。

提前谢谢你!

【问题讨论】:

    标签: ruby-on-rails ruby sqlite routes gem


    【解决方案1】:

    顺便说一句,您是否在启动服务器之前运行了bundle exec rake db:setup?看起来您的数据库还不存在(您可以检查db/development.sqlite3 文件是否存在)。

    【讨论】:

    • 感谢您的回答,但运行bundle exec rake db: setup 不起作用,我得到的和以前一样。 db/development.sqlite3 确实存在,但它是空的。有什么想法吗?
    • 它不起作用还是什么都不做?它是db:setup(里面没有空格)。也可以试试rake db:create
    • 此外,该文件不应为空——您应该能够使用一些 sqlite 编辑器打开它,例如sqlitebrowser.org
    • 是的,我正确地写了db:setuprake db:create 给我:db/development.sqlite3 already exists db/test.sqlite3 already exists
    • 我解决了,更改了我的 gemfile。无论如何感谢您的帮助。
    【解决方案2】:

    我自己解决了。这是由损坏的 Gemfile 引起的问题。 允许我让它工作的正确配置是:

    宝石文件:

    source 'https://rubygems.org'
    
    
    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
     gem 'rails', '4.2.2'
    # Use sqlite3 as the database for Active Record
     gem 'sqlite3'
    # Use SCSS for stylesheets
     gem 'sass-rails', '~> 5.0'
    # Use Uglifier as compressor for JavaScript assets
     gem 'uglifier', '>= 1.3.0'
    # Use CoffeeScript for .coffee assets and views
     gem 'coffee-rails', '~> 4.1.0'
    # See https://github.com/rails/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', '~> 2.0'
    # bundle exec rake doc:rails generates the API under doc/api.
     gem 'sdoc', '~> 0.4.0', group: :doc
    
    # Use ActiveModel has_secure_password
    # gem 'bcrypt', '~> 3.1.7'
    
    # Use Unicorn as the app server
    # gem 'unicorn'
    
    # Use Capistrano for deployment
    # gem 'capistrano-rails', group: :development
    
     group :development, :test do
    # Call 'debugger' anywhere in the code to stop execution and get a debugger console
       gem 'debugger'
    
    # Access an IRB console on exception pages or by using <%= console %> in views
       gem 'web-console', '~> 2.0'
    
    # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
       gem 'spring'
     end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多