How to deploy Rails App on Heroku?
Making your projects live is probably the best way to demo your work to the recruiters. Initially, we all start by running our projects on the localhost, which works just fine. Yet, when it comes to demoing the project to the recruiters, it is more convenient to have a live site. It will allow you to access your project site on any computer, at any time and/or place as long as you have an internet connection. You will not have to walk around with a laptop to demonstrate your work.
Now, if you’re wondering how can you make your site live on the internet, without having to pay, you’re solution is Heroku.
What is Heroku?
Heroku is a cloud-based platform that enables developers to build, run, and operate applications. Our aim is to host the application on Heroku’s web server, which could be very time consuming if we had to do it from scratch. Although it took me hours to successfully deploy my first Rails app to Heroku, I am sure if I had to implement it from scratch, it would have taken longer.
There are few initial steps that you’ve to complete before start working toward our main aim, hosting your Ruby on Rails application on Heroku.
- Sign up for an account on Heroku for free.
- Install Heroku Command Line Interface (CLI), it enables developers to locally set up the Heroku apps from the terminal.
- Install Postgres, it is important to use PostgreSQL to deploy applications on Heroku.
Now, if you’re done with all the installations, let’s move on to the fun part!
Step 1: Setup Heroku Locally
In order to run Heroku locally, login to your Heroku account from the terminal.
heroku login
Step 2: Create a new Rails app
Create the app and move into the root directory.
rails new myapp --database=postgresql
This enables us to use PostgreSQL with the Ruby on Rails application. It is important to use PostgreSQL on Heroku.
Step 3: Database
If you’re trying to deploy an existing application which you created without specifying the PostgreSQL database, then edit your gem file by adding the PostgreSQL gem and removing gem 'sqlite3'
from the Rails project.
gem 'pg'
Note: Don’t forget to run bundle install
.
Create the model, controller, view, routes, and seeds of your Rails application if you haven’t already.
Step 4: GitHub Repository
Add the project to a GitHub repository and commit the changes
git init
git add .
git commit -m "deploy app to heroku"
git push origin master
Step 5: Create an Application on Heroku
Finally, we can get started with the deployment of the application. Be in the root directory of the application, then run the following:
heroku create projectName
If you run heroku create
to build the app, Heroku will auto-generate a domain name for the URL of your application. It is recommended to use a domain that relates to your application.
Step 6: Push the app to Heroku
In order to deploy the application, you have to push all the changes to the master file in Heroku. It is very similar to GitHub.
Similar to GitHub, add the changes to your local repository by running git add .
, commit the changes git commit -m "deploy my app to heroku"
, and push the app to Heroku master with the following command:
git push heroku master
Note: In order to push the code in theheroku master
branch, you’ve to be in your GitHub master branch. If you want to use a different branch to deploy your project in Heroku run the following command:
git push heroku nameOfTheBranch:master
At this point, if you run into any errors, check if you’re in the correct directory, or if you committed all the changes. If you’re still stuck, ask google or StackOverflow for help!
Step 7: Rails Database Migration
Finally, manually seed and migrate the database to Heroku:
heroku run rake db:seed
heroku run rake db:migrate
To view the application, simply run heroku open
in your terminal.
Bonus:
If you have any images in your application that seem to break in your Heroku site, modify the config/production.rb
file by setting the following line to true.
config.assets.compile = false
to
config.assets.compile = true
Happy coding!
Previous posts:
How to Make Your LinkedIn Profile Stand Out & Get Better Opportunities in 2020?
How to Create Simple Forms in React?
How To Solve Loop Detection Interview Question? | Crack The Coding Interview (6th edition)
Markdown — A Easier & Fast to Learn Markup Language
Data Structure — Linked List| SINGLY LINKED LIST PRACTICE(LeetCode)
Debug JS Code with DevTool Network Tab