私は自分のアプリケーションをローカルで開発し、sqlite3をデータベースとして使用して、sqlite3がherokuで使用できないことを学びました。そこで、開発環境と本番環境の両方でpostgresqlを使用することにしました。すべての環境で同じデータベースを使用すると、作業が楽になると言われました。
postgresqlをインストールしました:
apt-get install postgresql
私のgemfileの関連部分は次のようになります。
group :development, :test do
gem 'pg', '0.13.2'
end
group :production do
gem 'pg', '0.13.2'
end
次に、バンドルインストールを実行しました。
database.ymlでは、3つの環境すべてが同じように見えます。
development:
adapter: postgresql
encoding: unicode
database: mydb
pool: 5
username: postgres
password: a
host: localhost
少しいじって、アプリケーションでpostgresデータベースをローカルで使用できるようにしました。完璧に動作します。herokuにプッシュしました。herokuでアプリケーションにアクセスしようとすると、次のように表示されます。
Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.
If you are the application owner, check your logs for details.
この問題はログファイルに関連していることを私は知っています。
これは、herokuログが言うことです:
2012-04-28T18:54:22+00:00 heroku[web.1]: State changed from crashed to created
2012-04-28T18:54:22+00:00 heroku[web.1]: State changed from created to starting
2012-04-28T18:54:29+00:00 heroku[web.1]: Starting process with command `bundle exec rails server -p 21268`
2012-04-28T18:54:35+00:00 app[web.1]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/config/environment.rb:5)
2012-04-28T18:54:35+00:00 app[web.1]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/config/environment.rb:5)
2012-04-28T18:54:41+00:00 app[web.1]: => Booting WEBrick
2012-04-28T18:54:41+00:00 app[web.1]: => Rails 3.2.3 application starting in production on http://0.0.0.0:21268
2012-04-28T18:54:41+00:00 app[web.1]: => Call with -d to detach
2012-04-28T18:54:41+00:00 app[web.1]: => Ctrl-C to shutdown server
2012-04-28T18:54:41+00:00 app[web.1]: Exiting
2012-04-28T18:54:41+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/rack/log_tailer.rb:8:in `size': No such file or directory - log/production.log (Errno::ENOENT)
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/rack/log_tailer.rb:8:in `initialize'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:295:in `new'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:291:in `reverse_each'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:295:in `block in build_app'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:291:in `build_app'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/commands/server.rb:70:in `start'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:55:in `block in <top (required)>'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:50:in `tap'
2012-04-28T18:54:41+00:00 app[web.1]: from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:50:in `<top (required)>'
2012-04-28T18:54:41+00:00 app[web.1]: from script/rails:6:in `require'
2012-04-28T18:54:41+00:00 app[web.1]: from script/rails:6:in `<main>'
2012-04-28T18:54:42+00:00 heroku[web.1]: Process exited with status 1
2012-04-28T18:54:42+00:00 heroku[web.1]: State changed from starting to crashed
2012-04-28T18:54:45+00:00 heroku[router]: Error H10 (App crashed) -> GET fierce-winter.herokuapp.com/ dyno= queue= wait= service= status=503 bytes=
SOに関する同様の質問では、バンドルインストールを再実行し、コミットにgemfileとgemfile.lockを含めるだけで済みます。その質問を見つける前でさえ、両方をしていました。出典:rails-3-2-from-sqlite-locally-to-postgres-on-heroku
編集:エラーログには、log/production.logファイルがないと表示されます。それが問題になることはありませんね。
編集2: production.rb:
SampleApp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
config.logger = Logger.new(STDOUT) ##
config.log_level = :info ##
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Prepend all log lines with the following tags
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
# config.active_record.auto_explain_threshold_in_seconds = 0.5
end
編集3:アプリをherokuにプッシュしたときに表示されるログ
root@user-VirtualBox:/home/user/RoR/rtapr22# git push heroku
Enter passphrase for key '/root/.ssh/id_rsa':
Counting objects: 52, done.
Compressing objects: 100% (41/41), done.
Writing objects: 100% (41/41), 8.21 KiB, done.
Total 41 (delta 13), reused 0 (delta 0)
-----> Heroku receiving push
-----> Ruby/Rails app detected
-----> Installing dependencies using Bundler version 1.1.2
Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
Fetching gem metadata from https://rubygems.org/.......
Fetching gem metadata from https://rubygems.org/..
Using rake (0.9.2.2)
Using i18n (0.6.0)
Using multi_json (1.3.2)
Using activesupport (3.2.3)
Using builder (3.0.0)
Using activemodel (3.2.3)
Using erubis (2.7.0)
Using journey (1.0.3)
Using rack (1.4.1)
Using rack-cache (1.2)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.2)
Using actionpack (3.2.3)
Using mime-types (1.18)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.4.4)
Using actionmailer (3.2.3)
Using arel (3.0.2)
Using tzinfo (0.3.33)
Using activerecord (3.2.3)
Using activeresource (3.2.3)
Using addressable (2.2.7)
Using bcrypt-ruby (3.0.1)
Using bootstrap-sass (2.0.0)
Using will_paginate (3.0.3)
Using bootstrap-will_paginate (0.0.5)
Using coffee-script-source (1.3.1)
Using execjs (1.3.0)
Using coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.6.6)
Using rdoc (3.12)
Using thor (0.14.6)
Using railties (3.2.3)
Using coffee-rails (3.2.2)
Using curb (0.8.0)
Using orm_adapter (0.0.7)
Using warden (1.1.1)
Using devise (2.0.4)
Using faker (1.0.1)
Using nokogiri (1.5.2)
Using loofah (1.2.1)
Using sax-machine (0.1.0)
Using feedzirra (0.0.24)
Using launchy (2.1.0)
Using netrc (0.7.1)
Using rest-client (1.6.7)
Using rubyzip (0.9.7)
Using heroku (2.24.1)
Using jquery-rails (2.0.0)
Using libv8 (3.3.10.4)
Using pg (0.13.2)
Using bundler (1.1.2)
Using rails (3.2.3)
Installing rails_log_stdout (0.1.1)
Using rb-readline (0.4.2)
Using sass (3.1.15)
Using sass-rails (3.2.4)
Using therubyracer (0.10.1)
Using uglifier (1.2.3)
Your bundle is complete! It was installed into ./vendor/bundle
Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
Compiled jquery.js (2ms) (pid 677)
Compiled jquery_ujs.js (0ms) (pid 677)
Compiled bootstrap-transition.js (0ms) (pid 677)
Compiled bootstrap-alert.js (0ms) (pid 677)
Compiled bootstrap-button.js (0ms) (pid 677)
Compiled bootstrap-carousel.js (0ms) (pid 677)
Compiled bootstrap-collapse.js (0ms) (pid 677)
Compiled bootstrap-dropdown.js (0ms) (pid 677)
Compiled bootstrap-modal.js (0ms) (pid 677)
Compiled bootstrap-scrollspy.js (0ms) (pid 677)
Compiled bootstrap-tab.js (0ms) (pid 677)
Compiled bootstrap-tooltip.js (28ms) (pid 677)
Compiled bootstrap-popover.js (0ms) (pid 677)
Compiled bootstrap-typeahead.js (0ms) (pid 677)
Compiled bootstrap.js (97ms) (pid 677)
Compiled sessions.js (84ms) (pid 677)
Compiled static_pages.js (0ms) (pid 677)
Compiled users.js (0ms) (pid 677)
Compiled application.js (240ms) (pid 677)
Compiled Untitled Folder/custom.css (1321ms) (pid 677)
Compiled Untitled Folder/sessions.css (1ms) (pid 677)
Compiled Untitled Folder/static_pages.css (0ms) (pid 677)
Compiled Untitled Folder/users.css (0ms) (pid 677)
Compiled Untitled Folder/application.css (1377ms) (pid 677)
Compiled custom.css (512ms) (pid 677)
Compiled sessions.css (1ms) (pid 677)
Compiled static_pages.css (1ms) (pid 677)
Compiled users.css (0ms) (pid 677)
Compiled application.css (533ms) (pid 677)
Compiled jquery.js (1ms) (pid 677)
Compiled jquery_ujs.js (0ms) (pid 677)
Compiled bootstrap-transition.js (0ms) (pid 677)
Compiled bootstrap-alert.js (0ms) (pid 677)
Compiled bootstrap-button.js (0ms) (pid 677)
Compiled bootstrap-carousel.js (0ms) (pid 677)
Compiled bootstrap-collapse.js (0ms) (pid 677)
Compiled bootstrap-dropdown.js (0ms) (pid 677)
Compiled bootstrap-modal.js (0ms) (pid 677)
Compiled bootstrap-scrollspy.js (0ms) (pid 677)
Compiled bootstrap-tab.js (0ms) (pid 677)
Compiled bootstrap-tooltip.js (0ms) (pid 677)
Compiled bootstrap-popover.js (0ms) (pid 677)
Compiled bootstrap-typeahead.js (0ms) (pid 677)
Compiled bootstrap.js (79ms) (pid 677)
Compiled sessions.js (81ms) (pid 677)
Compiled static_pages.js (0ms) (pid 677)
Compiled users.js (0ms) (pid 677)
Compiled application.js (217ms) (pid 677)
Compiled Untitled Folder/custom.css (1291ms) (pid 677)
Compiled Untitled Folder/sessions.css (1ms) (pid 677)
Compiled Untitled Folder/static_pages.css (0ms) (pid 677)
Compiled Untitled Folder/users.css (1ms) (pid 677)
Compiled Untitled Folder/application.css (1311ms) (pid 677)
Compiled custom.css (545ms) (pid 677)
Compiled sessions.css (1ms) (pid 677)
Compiled static_pages.css (0ms) (pid 677)
Compiled users.css (0ms) (pid 677)
Compiled application.css (566ms) (pid 677)
-----> Rails plugin injection
Injecting rails_log_stdout
Injecting rails3_serve_static_assets
-----> Discovering process types
Procfile declares types -> (none)
Default types for Ruby/Rails -> console, rake, web, worker
-----> Compiled slug size is 26.1MB
-----> Launching... done, v18
http://fierce-winter.herokuapp.com deployed to Heroku
To git@heroku.com:fierce-winter.git
24d1b1f..78c2b86 master -> master