Use heroku logs
Heroku logs often provide in-depth useful information for debugging your rails apps, it shows us the exact block of code where the bug exists, sometimes it can be because of a typo or a problem with gem versions or something else.
We can also use heroku logs with –tail to continuously get live logs. We can also check our logs from the heroku dashboard, but its better to just use heroku cli. Here’s the complete guide on logging https://devcenter.heroku.com/articles/logging
Use latest version of Ruby and Ruby On Rails
Heroku has suspended support for old versions of ruby/ruby on rails, if you are using an older version of rails, consider upgrading it to the latest version such as rails6, upgrading rails version can sometimes be complicated, because of different dependencies and also because the methods used in earlier versions of rails has changed a lot in never version of rails.
Before upgrading your rails apps consider checking out the complete documentation on upgrading your rails version and also you can also checkout info on heroku stacks here: https://devcenter.heroku.com/articles/stack
Problems with gems
Check your rubygems for problems, some of the gems just don’t seem to work on heroku, gems such as ‘sqlite3’. If you are developing on a windows platform remove the ‘tzinfo-data’ gem, before deploying, also check if the version of your gems works with no issues with your current version of rails.
#File: Gemfile
#Just comment out the gems that doesn't work with heroku then run bundler and push the changes.
#gem 'sqlite3', '~> 1.4'
#gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Problem with assets
Most of the time heroku will fail to deploy because of problems with your assets, make sure to precompile your assets locally before pushing to heroku, you can even host your assets completely on a separate server or CDN, to make your app load even faster, here’s a tutorial on how to do so
Don’t use sqlite3
Don’t use sqlite on production environment, heroku doesn’t support sqlite, you can replace sqlite with heroku postgres or mysql or you can even use external databases such as amazon RDS or Digitalocean databases, or something similar to that.
You can use DATABASE_URL env variable to do so, heroku adds it by default when you use heroku postgres, and don’t forget to make changes to your database.yml file
#File: Gemfile
#Don't use sqlite3
#gem 'sqlite3', '~> 1.4'
#use postgres
group :production do
gem 'pg'
end
#File: config/database.yml
production:
url: <%= ENV['DATABASE_URL'] %>
Pending Database Migrations
After deploying your rails apps, if it shows up ‘500’ error, it’s most probably because you forgot to run database migrations, so go ahead and run the migrations using the command below
heroku run rake db:migrate
Check your environment variables
Check whether if you have added all the required environment variables for your app, and also check if there’s any typos on the values. If you are using Amazon S3 or something similar to store objects make sure to add the correct Environment variables, such as ACCESS_KEY_ID, SECRET_KEY, etc.
These were the most common ways that i figured out to help you deploy your rails apps on heroku with zero fails. If there’s any other issues you have faced or if you know a better way to fix those issues, please let me know.