Deploying A Rails App To Heroku

From development to production – best known as another phase in the app designing process for your perfectly fine development code to error out and blow up on you. Deploying to Heroku is supposed to make this easier. Most of the time this is true, sometimes though, it’s just not. Here is a simple guide for deploying a Rails app to Heroku.

First Some Prerequisites

  • You must have a Ruby version installed that is 2.0 or later.
  • Your app must run on a PostgreSQL database, otherwise you cannot deploy to Heroku. (If you are using a different database, there is way around this, and that’s mentioned at the bottom).
  • You must be connected to Github and have a repository already initialized.
  • You have a rails app created, a database created and migrated, and a root route set up.

Sign Up For Heroku

You must be signed up for Heroku. Go here if you’re not and sign up. https://www.heroku.com/ After you sign up you will need to download the Heroku tool belt, that can be done here. https://devcenter.heroku.com/articles/getting-started-with-ruby#set-up

Time to pull up your terminal and make sure you are cd’d into your app. Type this command and fill in the correct information when it asks for it.

heroku login

Prepping Your App For Heroku

In your Rails app go to your Gemfile and paste this

gem 'rails_12factor', group: :production

Back in the terminal it’s time to bundle

bundle install

It’s now time to push everything up into Git.

git add --all
git commit -m "Type your commit message here."
git push origin master

Deploying To Heroku

In your terminal, and only once will you ever have to type this in, type

heroku create

Then push your app onto the heroku master branch with

git push heroku master

Migrate your database with

heroku run rake db:migrate

If you want to rename your app, you can type

heroku apps:rename type-app-name-here

Open your app in the web browser by typing

heroku open

Voila!

Some Useful Heroku Commands

Now that you have a running app in Heroku, here are some other useful features you may want to use. All of these can be typed into the terminal.

To see error messages type

heroku logs

To seed any data you have type

heroku run rake db:seed

If you want to run a Rails console on your Heroku app, type

heroku run rails console

And should you decide it’s time to destroy your app, you can simply type

heroku apps:destroy --app type-app-name-here

You Don’t Have A PostgreSQL Database

Well there is still hope for you. BEFORE, you push up to Git, you can do the following:

In your app’s Gemfile add

gem 'pg', group: :production

In your app’s config folder open the database.yml file and find the production section and change it so it looks like this:

production:
 database: yourappname-production
 adapter: postgresql
 encoding: unicode
 pool: 5

In your terminal check and see if you have PostgreSQL installed with

psql --version

If you have PostgreSQL installed type

bundle install

If you don’t have PostgreSQL installed type

bundle install --without-production

Head to the Deploying to Heroku section and continue following the directions there.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s