As we discussed in the last article that we need to use PostgreSQL database in our project if we want to deploy the project successfully on Heroku, so we’d replace any other database gem with the following line:
In addition to using the pg gem, we also need to make sure that the database configuration file that stays at config/database.yml has also been configured properly.
Once we are done with these two steps, we shall move on to the next step that is: adding gems that Heroku depends upon for deployment, and the good news is that there isn’t much to install; you only have to write the following gem in your Gemfile and write the group as production with it.
gem `rails_12factor`, group: :production
Heroku knows which version of Ruby you are using by taking a look at your Gemfile. So you need to specify the ruby version of your project in Gemfile in the following way:
That’s it for the necessary configuration. It’s time to push the changes, and if you are good at Git, or at least know its basics, you would be good to go with it.
git push heroku master
It may take a while as it will install all the necessary gems and their dependencies for our application on Heroku, and it is also wise to have an eye on the outputs logs to see if there is any warning or errors.
If everything went well, it is time for migrating the database. Migrating the database on Heroku is the same as on local except you have to append heroku run at the beginning of the command. So it makes:
heroku run rake db:migrate
Before we move on explaining how actually to visit your application on Heroku in your browser, it is worthwhile to explore an important concept here, something known as Dyno.
According to Heroku, “a dyno is a lightweight Linux container that runs a single user-specified command.” But there is more than this, and there are types of dyno as well so we wouldn’t go into the explanation of what exactly it is, and how it affects our services. Instead, we’ll keep ourselves focused on running a rails application on Heroku.
After we are done with the migration of the database, it is finally to see our web application live on Heroku.
It will open up your web application in the default browser on your local machine.
Congratulations! You have successfully deployed your application on Heroku, and it is all good.
Similarly, if you make any changes in future, in your Rails application, and you would like to see those changes live on Heroku; all you have to do is to push the changes to the remote named heroku, the same way you push changes to any remote in Git.
At any moment, if you would like to see the logs of your rails application at Heroku, you can invoke the following command:
It will show you the recent logs of your application, but it will only show you the last 100 lines of log at the time of you invoking heroku logs command.
If you would like to see more than that, you can do:
heroku logs -n 1500
Even better, if you would like to see the live logs, you can run the following command in your terminal:
heroku logs -t
Last, but not least – if you would like to do something console-based on Heroku, you can open a new rails console shell through the following command:
heroku run rails console
So that’s it for deploying a rails application successfully to Heroku, and if you would like to learn more, you can surely head over to official guides at Heroku at this link. Did we miss anything or there is anything you would like to see added to this blog/article, do let us know in the comments section below. Happy deploying!