我是Rails的新手。一切都很好,直到我改变我的Gemfile才能使用heroku。我在我的Gemfile中有pg,但在运行bundle exec rake test
我也不能使用rails控制台。
rake aborted!
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:44:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/base.rb:316:in `<module:ActiveRecord>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/base.rb:26:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:411:in `maintain_test_schema!'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_help.rb:19:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/workspace/sample_app/test/test_helper.rb:3:in `<top (required)>'
/home/rihanna/workspace/sample_app/test/controllers/static_pages_controller_test.rb:1:in `require'
/home/rihanna/workspace/sample_app/test/controllers/static_pages_controller_test.rb:1:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Gem::LoadError: pg is not part of the bundle. Add it to Gemfile.
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/bundler-1.8.5/lib/bundler/rubygems_integration.rb:261:in `block in replace_gem'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:16:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:175:in `spec'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:44:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/base.rb:316:in `<module:ActiveRecord>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/base.rb:26:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:411:in `maintain_test_schema!'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_help.rb:19:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
/home/rihanna/workspace/sample_app/test/test_helper.rb:3:in `<top (required)>'
/home/rihanna/workspace/sample_app/test/controllers/static_pages_controller_test.rb:1:in `require'
/home/rihanna/workspace/sample_app/test/controllers/static_pages_controller_test.rb:1:in `<top (required)>'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/home/rihanna/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
这是我的Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use sqlite3 as the database for Active Record
#gem 'pg'
# 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/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', '~> 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 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'sqlite3'
# 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
group :production do
gem 'pg', '0.18.1'
gem 'rails_12factor', '0.0.2'
end
答案 0 :(得分:0)
请替换以下
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'sqlite3'
# 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
由此
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'pg'
# 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
对于开发和测试,您需要gem pg
用于postgresql适配器而不是gem sqlit3
答案 1 :(得分:0)
类似的问题,尽管Gemfile是正确的(pg在produciton中,而sqlite在dev中,database.yml有postgresql)。捆绑拒绝接载pg gem。
我正在编写heroku实例的创建脚本,所以这一切都在cli
上我的解决方案
明确添加postgress加载项(heroku addons:create heroku-postgresql:hobby-dev --app <myappname>
)。这似乎迫使它尽早创建DATABASE_URL(Created postgresql-xxxxxxx-9999 as DATABASE_URL
)。捆绑似乎有效。
我考虑/尝试过并为每个环境创建保留的其他内容,是为了确保您在过程中尽快设置变量heroku config:set --app <mayappname> RAILS_ENV=production