Last week, I created a quick tutorial about switching a Ruby on Rails database from SQLite3 to PostgreSQL, which was step 1 of 2 to this overall Heroku deployment tutorial. Today, I am here to give y’all the second portion of this tutorial.
To build on what I was explaining in the previous post, I was really nervous when I first set out to deploy my Ruby on Rails project with Heroku. There was a lot of talk in my bootcamp of people struggling to do this, and for the most part it was in regards to the database switch. Thankfully, I essentially made it through that without any big issues, but I definitely hit a few more snags while attempting to deploy to Heroku.
I am going to do my best at including all of the tiny snags I hit and had to Google, that way this can hopefully be a one-stop-shop for those of us who are new to deploying via Heroku. Let’s get started.
First off, you need to install the Heroku CLI on your machine. Once you have it installed, you need to open your terminal and log in to your Heroku account by using the command
$ heroku login.
You will be prompted to press any key to login. I always choose to press
enter. Once you've done this, you'll be redirected to the web browser.
When you press login and you've successfully done so, you'll be prompted to return back to the terminal and it should look like this (without all the blue markings, as I'm just trying to protect my login information).
One important thing to note if you are upgrading an old project, you will need to make sure that you are using an upgraded version of Rails. As of now, Heroku accepts Rails 5. If you are unsure of how to check which version your project currently uses, go to your terminal, make sure you are within the project directory, and run
$ rails -v. If you are using a version older than Rails 5, please make sure to click the link above for instructions to upgrade.
If you are creating a new project, please make sure to create it with a PostgreSQL database so that you can avoid the need to switch it over later on. You can do this by using the following command in your terminal:
$ rails new app-name -- database=postgresql
Make sure to enter your project directory after you’ve created it by running the command
$ cd app-name.
If you just created a new project using PostgreSQL as your database, you can skip over this section.
If you have an existing project using a SQLite database, this is the point where we need to make the change to PostgreSQL. Why you may ask? Well, Heroku does not support a SQLite database and you will be unable to deploy your project. You can go check out the first half of this 2 step tutorial that I’ve written to make this change happen.
You’ll want to have a root page for your project, which will function as the initial landing page of the web-application when it opens up once deployed via Heroku. In order to accomplish this, you’ll need to make sure you have a controller for the root page as well as an index page.
For this tutorial, we are going to create a
home controller. You can create it by using the following command in the terminal:
$ rails g controller home
Now, we need to create the index page by creating a new file (
app/views/home/index.html.erb). Within this file you can write/code whatever you’d like to be on the home page.
Once you’ve got that all completed, you’ll need to add a Rails route for it within
You’ll want to make sure this is working properly by running your server using
$ rails s and visiting http://localhost:3000 in your browser.
According to the Heroku official documents, “previous versions of Rails required you to add a gem to your project rails_12factor to enable static asset serving and logging on Heroku.” If you created a new application, you can ignore this gem. If you are upgrading an existing project and have this gem within your
Gemfile, you may remove it, provided you have this configuration in your
I did not personally have to change anything in regards to the above information, but I did hit another snag in regards to my
production.rb file. When I went to deploy my project, I kept getting an error similar to
Ugligier::Error: Unexpected token: punc((). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).
After spending a few minutes googling, I discovered that the error was within my
It needed to look like this
So, if you come across a similar issue, just double check your
Now, as I said in the previous tutorial, please PLEASE PLEASE make sure you are committing and pushing your code as often as possible. I kept making the mistake of trying to push to Heroku and having the same errors keep occurring, because I wasn’t committing and pushing my changes to Git and GitHub.
Now, for the fun part! Within the terminal of your project directory, create an app on Heroku by running
$ heroku create.
After you’ve done this, deploy your code with the following command:
$ git push heroku master
If you come across any errors, just double check to make sure you have committed all of your changes to Git. If that isn’t the cause, then Google will be your best friend!
You will need to manually migrate your database by running this command
$ heroku run rake db:migrate
Now you are ready to see your application live on Heroku!!! In order to do this, run
$ heroku open in your terminal.
Obviously, I won’t have all the answers for every issue that someone could possibly come by, so please Google if needed. I wanted to make sure to include the issues that I personally came across so that I can help people who hit the same snags I did.
I hope that with this tutorial I have made deploying to Heroku a little less scary and a lot smoother of an experience for those of you reading this.