将Ruby on Rails应用推送至Heroku时遇应用错误求助
Hey there! Let's figure out why your Rails app is throwing an error on Heroku even after adding the pg and rails_12factor gems—especially since you had this working just a few weeks ago. Here are the most common fixes to try, tailored to your scenario:
1. Ensure Your Gemfile & Gemfile.lock Are In Sync
It’s easy to add gems to your Gemfile but forget to update the lock file, which Heroku relies on to install dependencies correctly.
- Run
bundle installlocally to generate an updatedGemfile.lockthat includespgandrails_12factor. - Double-check that
pgis in the production group in your Gemfile (since you don’t need it for local development with SQLite):group :production do gem 'pg' gem 'rails_12factor' end - Commit both
GemfileandGemfile.lockto git before pushing to Heroku.
2. Run Database Migrations on Heroku
Oftentimes, deployment errors stem from missing database migrations. Even if your schema looks fine locally, Heroku’s production database is separate:
- After pushing your code, run:
heroku run rails db:migrate - Then restart your app to apply changes:
heroku restart - Check for migration failures by tailing your logs:
heroku logs --tail—look for lines likePG::UndefinedTableorActiveRecord::MigrationMissing.
3. Fix Version Conflicts (Ruby/Rails)
Since it worked weeks ago, a recent update to your local Ruby, Rails, or other gems might be causing compatibility issues with Heroku’s environment:
- Verify your local Ruby version matches what Heroku is using. Add this line to your Gemfile to lock it in (replace with your local version):
ruby '3.2.2' - Check Heroku logs for lines like
Bundler could not find compatible versions for gem "rails"—this means a gem dependency is pulling in a conflicting Rails version.
4. Replace Deprecated rails_12factor (For Rails 5+)
Heads up: rails_12factor is deprecated for Rails 5 and newer. Heroku now handles static assets and logging natively, so you can:
- Remove
gem 'rails_12factor'from your Gemfile. - Ensure static assets are served in production by adding this to
config/environments/production.rb:config.public_file_server.enabled = true - Re-run
bundle install, commit changes, and push again.
If you can share specific snippets from your push output or Heroku logs (like the exact error message), we can narrow this down even further!
内容的提问来源于stack exchange,提问作者user8556255

