DEV Community

Tori Crawford
Tori Crawford

Posted on • Edited on

Deploying a Ruby on Rails Project With Heroku

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.

Local Setup of Heroku

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.

terminal screenshot for local setup

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.

screenshot of initial web browser heroku login

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).

screenshot of terminal after successful login

Create a New (or Upgrade an Old) Ruby on Rails Project

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 
Enter fullscreen mode Exit fullscreen mode

Make sure to enter your project directory after you’ve created it by running the command $ cd app-name.

Database

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.

Create a Root/Home Page

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
Enter fullscreen mode Exit fullscreen mode

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 config/routes.rb. In config/routes.rb add

root 'home#index'
Enter fullscreen mode Exit fullscreen mode

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.

production.rb File

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 config/environments/production.rb file:

screenshot of production.rb file

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 config/environments/production.rb file.

At first my JavaScript compressor line looked like

screenshot of production.rb file

It needed to look like this

screenshot of production.rb file

So, if you come across a similar issue, just double check your production.rb file.

Git

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.

Deploying to Heroku

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
Enter fullscreen mode Exit fullscreen mode

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!

Database Migration

You will need to manually migrate your database by running this command

$ heroku run rake db:migrate
Enter fullscreen mode Exit fullscreen mode

Visit Your Project

Now you are ready to see your application live on Heroku!!! In order to do this, run $ heroku open in your terminal.

Final Regards

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.

Happy coding!!

Sources

Getting Started on Heroku with Rails 5.x
Heroku push issue
Uglifier::Error

Top comments (1)

Collapse
 
fentybit profile image
fentybit • Edited

THANK YOU for your blog, it helped me to deploy my first app on Heroku! :)