Pushing a Rails app to Heroku

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

Setup

Let’s generate the bare app first:

1

rails newBlog--skip-test-unit--skip-bundle-dpostgresql

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:

1

2

3

4

5

6

7

8

9

10

group:development,:test do

gem'rspec-rails'

gem'faker'

gem'factory_girl_rails'

end

group:test do

gem'capybara'

gem'shoulda'

end

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

TDD

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

1

2

3

4

5

6

7

8

9

10

11

12

13

# spec/features/homepage_spec.rb

feature'visits homepage'do

scenario'sees welcome text'do

visit'/'

expect(page).tohave_selector'h1',text:'Welcome to Sample blog'

end

scenario'sees link for the Blog index page'do

visit'/'

expect(page).tohave_selector"a[href='#{blogs_path}']",text:'Blog'

end

end

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

1

2

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

1

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

1

2

classPagesController&lt;ApplicationController

end

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

1

2

3

4

classPagesController&lt;ApplicationController

def home

end

end

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

1

<h1>Welcome toSample blog</h1>

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

…..

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

1

2

3

git init

git add.

git commit-m"Finished blog."

Heroku

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)