部署Ruby应用到Heroku时出现ActionView错误(SQL/PostgreSQL)
Hey there, let's walk through the most common fixes for that ActionView error you're facing. First, let's start with the basics since your Gemfile setup is partially done, but we might be missing a few key pieces.
1. Double-Check Your Production Database Gem Configuration
It looks like you've set up sqlite3 for development/test, but I don't see the pg gem specified for production in the code snippet you shared. Heroku requires PostgreSQL for production, so make sure your Gemfile includes this:
group :production do gem 'pg' end
After adding this, run bundle install --without production locally to avoid installing pg on your dev machine, then commit the updated Gemfile and Gemfile.lock to Git before redeploying.
2. Ensure Asset Precompilation Works for Production
ActionView errors often pop up when Rails can't find compiled assets (like CSS/JS files) in production. Here's how to fix that:
- First, test precompiling assets locally in production mode:
RAILS_ENV=production bundle exec rake assets:precompile - This will generate a
public/assetsdirectory. Add this directory to Git (don't ignore it!) and commit the changes. - If you don't want to precompile locally, you can let Heroku handle it, but make sure your
config/environments/production.rbhas these settings:config.assets.compile = true # Fallback if assets aren't precompiled config.public_file_server.enabled = true
3. Run Database Migrations on Heroku
Forgot to run migrations after deploying? That can cause ActionView errors if your templates rely on database columns that don't exist yet. After deploying, run:
heroku run rails db:migrate
You can also add a release phase to your Procfile to auto-run migrations on deploy:
release: bundle exec rails db:migrate web: bundle exec rails server -p $PORT
4. Dig Into the Exact Error Details
"ActionView error" is pretty broad—let's get specific. Run this command to see real-time logs from Heroku:
heroku logs --tail
Look for lines starting with ActionView::Template::Error—it'll tell you exactly what's wrong (e.g., missing template, undefined variable, broken asset path). That's the fastest way to pinpoint the root cause.
5. Verify SQLite3 Isn't Leaking Into Production
Make sure the sqlite3 gem is only in the :development, :test group (like you have) and not listed outside any group. Heroku will try to install all gems not restricted to other groups, and SQLite doesn't work on Heroku's production environment.
内容的提问来源于stack exchange,提问作者Sox -

