Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Tori Crawford
Tori Crawford

Posted on • Edited on

     

Deploying a Ruby on Rails Project With Heroku

Last week, I created a quicktutorial 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 theHeroku 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 pressenter. 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 acceptsRails 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 modeExit 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 thefirst 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 ahome controller. You can create it by using the following command in the terminal:

$ rails g controller home
Enter fullscreen modeExit 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 withinconfig/routes.rb. Inconfig/routes.rb add

root'home#index'
Enter fullscreen modeExit fullscreen mode

You’ll want to make sure this is working properly by running your server using$ rails s and visitinghttp://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 projectrails_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 yourGemfile, you may remove it, provided you have this configuration in yourconfig/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 myproduction.rb file. When I went to deploy my project, I kept getting an error similar toUgligier::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 myconfig/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 yourproduction.rb file.

Git

Now, as I said in the previous tutorial, please PLEASEPLEASE 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 toGit 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 modeExit 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 modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
fentybit profile image
fentybit
full stack engineer 〰️
  • Location
    Los Angeles, CA
  • Education
    Flatiron School
  • Work
    Full Stack Software Engineer at RE/MAX
  • Joined
• Edited on• Edited

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

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A software engineer who loves learning and sharing knowledge.
  • Location
    Boise, ID
  • Education
    M.S.
  • Pronouns
    she/her
  • Work
    Full Stack Software Engineer - Looking for Next Role
  • Joined

More fromTori Crawford

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp