Pushing a Rails app to Heroku

heroku-Logo-1We will be creating a very simple blog using Rails 4, RSpec then deploy it to Heroku today, mainly focus on the way to implement TDD. The blog will use postgresql to be able to push to Heroku. We’re going to get rid of some defaults on generating the application.


Let’s generate the bare app first:

rails new Blog --skip-test-unit --skip-bundle -d postgresql

what we have now is a skeleton without `test` directory (as we plan to use `RSpec` instead), configured to use postgresql (config/database.yml, `pg` gem automatically added to Gemfile) and `bundle install` didn’t run yet.

Then go add these gems to the Gemfile:

group :development, :test do
  gem 'rspec-rails'
  gem 'faker'
  gem 'factory_girl_rails'

group :test do
  gem 'capybara'
  gem 'shoulda'

Now  run `bundle install` and install RSpec: `rails g rspec:install`. It will generate the `spec` directory.


Let’s start with spec by writing a `feature` spec using RSpec and Capybara.

# spec/features/homepage_spec.rb

feature 'visits homepage' do
  scenario 'sees welcome text' do
    visit '/'
    expect(page).to have_selector 'h1', text: 'Welcome to Sample blog'

  scenario 'sees link for the Blog index page' do
    visit '/'
    expect(page).to have_selector "a[href='#{blogs_path}']", text: 'Blog'

Remember to fill the postgresql credentials in `config/database.yml` and run these commands to prepare for test database:

rake db:create
rake db:test:prepare

Run `rspec spec` to see it goes red.

Ahhhh, we don’t have the root path yet, let’s go define it in `config/routes.rb` then run spec again

root 'pages#home'

Ahhhh,  we don’t have the Pages controller. go to `app/controllers` and add a `pages_controller.rb` file with this content and run spec again

class PagesController < ApplicationController

Ahhhh, now it couldn’t find the `home` action, let’s add it to the class body

class PagesController < ApplicationController
  def home

Ahhhh, now it’s looking for the view template, let’s go add a template in `app/views/pages/home.html.erb` and run spec again.

It now looks for the welcome text and the link, let’s add them to the template

Welcome to Sample blog

So on, gradually we implement minimum code to pass the specs….


After we finish the blog, let’s initialize a git repo and commit.

git init
git add .
git commit -m "Finished blog."


We now have an application running on our local computer. Let’s deploy it to Heroku for the whole world to know who we are :))

First we need to install the Heroku toolbelt, download and install it here: https://toolbelt.heroku.com/

After installing heroku toolbelt, the `heroku` command will be available. We need to login, create and push the application
to Heroku. Be sure you have a Heroku account first (register one here if you haven”t http://heroku.com)

heroku login
heroku create sample-blog
git push heroku master

The blog is now available at http://septeni-blog.herokuapp.com/.


Add a Comment

Scroll Up