Developing Ruby on Rails Application for Heroku

This post describes how to setup an environment to develop a Ruby on Rails application and deploy it on Heroku.com.
Heroku is a Platform-as-a-Service (PaaS) that provides deployment environment on the cloud.
Ruby on Rails web application is well-supported by Heroku in addition to other environment, such as Java.
Utilizing Heroku and Git significantly simplifies the development and deployment process, otherwise very difficult tasks.

This tutorial extensibly uses Rails and Git, and focuses the development and deployment process.
See Setting Up Ruby on Rails on Windows and Git Basic Guide (on Windows) for more detail.

Below describes setting up the environment for the development and deployment.
GitHub is used as the central repository for the revision control.
Two Heroku applications/repositories are created for the staging and production deployment.
Staging is used as an intermediate testing environment, to avoid any unexpected interference for the production.

  1. Develop and test locally
  2. Deploy and test on staging environment
  3. Deploy on production environment

Initial Setup

  1. git config --global alias.commit-all '!git add -A && git commit'
    Create a command alias to commits all changes including created and deleted files.
    This is just to simplify the commit command (i.e., git commit-all), and only needs to be done once.
  2. Setup GitHub.com account if you haven't (including SSH Key).
    See Git Basic Guide (on Windows) for more detail.
  3. Setup Heroku.com account if you haven't.
    Also, go to https://heroku.com/confirm.

Application Setup

  1. rails new new-app-name
    Creates a new Rails application.

    rails new new-app-name --skip-test-unit
    Skips the default Test::Unit framework, and use Rspec.
  2. cd new-app-name
  3. Update Gemfile:

    Especially, enable 'sqlite3' gem only for development (heroku uses PostgreSQL).

    Also, add 'rspec-rails' and 'capybara' for testing.

    group :development, :test do gem 'sqlite3' gem 'rspec-rails' end group :production do gem 'pg' end group :test do gem 'capybara' end gem 'bootstrap-sass'
    [open]

    (May have to specify version for each gem to match with the deployment environment)

  4. bundle install --without production
    Insall gems specified in Gemfile.

    bundle install
    (after once)
  5. rails generate rspec:install
    Runs Rspec generator.

Repository Setup

  1. Setup Local Repository

    1. git init
      Creates an empty repository.
    2. git commit-all -m "Create new application"
      Commits all changes including created and deleted files.
  2. Setup GitHub Repository

    1. Create a new repository at GitHub.com.
    2. git remote add origin [email protected]:username/repository-name.git
      Adds an entry for a remote repository on GitHub and refer it as origin.
    3. git push -u origin master
      Pushes master to origin (on GitHub).

      git push
      Pushes master to origin (after pushing once).
  3. Setup Heroku Staging Application/Repository

    1. heroku create --stack cedar --remote staging
      Creates new Heroku application, and staging remote repository.
    2. heroke rename app-name-staging --remote staging
      Renames the staging application to app-name-staging (Must be unique on Heroku).
      (This is optional)
    3. git push staging master
      Pushes master to staging.

      git push staging
      Pushes master to staging (after pushing once).
    4. heroku open --remote staging
      Opens the staging application in the browser.
  4. Setup Heroku Production Application/Repository

    1. heroku create --stack cedar --remote production
      Creates new Heroku application, and production remote repository.
    2. heroke rename app-name-production --remote production
      Renames the production application to app-name-production (Must be unique on Heroku).
      (This is optional)
    3. git push production master
      Pushes master to production.

      git push production
      Pushes master to production (after pushing once).
    4. heroku open --remote production
      Opens the production application in the browser.
    5. Add CNAME entry for www.example.com (in your domain manager) pointing to app-name-production.herokuapp.com.
      (Optionally, add forward for naked domain to www subdomain)
    6. heroku domains:add www.example.com --remote production
      Adds the custom domain for the production application.
    7. Add www.example.com to www.wekkars.com (May need to use app-name-production.herokuapp.com).
      (Optional, but should do this for a free heroku account to keep the site active)

Development

  • git mv README.rdoc README.md
    Renames the README file Git-way.
  • Edit README.md.
  • Edit app/views/layouts/application.html.erb.
  • Add Controllers and Actions

    • rails generate controller ControllerNames action_name1 action_name2 --no-test-framework
      Creates a new controller with actions.
    • rails destroy controller ControllerNames action_name1 action_name2
      Reverts the controller creation.
    • Add an action to a controller:
      1. Add a 'get' line in config/routs.rb:

        AppName::Application.routes.draw do … root to: 'controller_names#action_name' resources :controller_names get 'controller_names/action_name' match '/some_name', to: 'controller_names#action_name' … end
        [open]
      2. Add a function in app/controller/controller_names.rb:

        class ControllerNamesController < ApplicationController … def action_name end … end
        [open]
      3. Create app/views/controller_names/action_name.html.erb.
  • Add Models

    • rails generate model ModelName field1:data-type field2:data-type
      Creates a new model with fields.
    • rails destroy model ModelName
      Reverts the model creation.
    • rails generate migration add_new_column_name_to_model_name new_column_name:data-type
      Adds a new column to attr_accessible in app/model/model_name.rb.
      It also adds a database migration file in db/migration.
    • Database Migration

      • bundle exec rake db:migrate
        Applies new migrations to the database.
        Any time model resources are added, the database needs to be update.
      • bundle exec rake db:rollback
        Reverts the database migration.
      • bundle exec rake db:migrate VERSION=0
        Restores the database to the initial version.
      • bundle exec rake db:reset
        Resets the database.
      • bundle exec rake db:test:prepare
        Prepares the test database.
  • Debug

    • From a controller:
      • render :text => "string"
      • render :text => params.inspect
    • From a view:
      • <%= debug "string" %>
  • Add Helpers

    • Create app/helpers/application_helper.rb
  • Add Integration Tests

    1. rails generate integration_test controller_names
    2. Modify spec/requests/controller_names_spec.rb
    3. bundle exec rspec spec/requests/controller_names_spec.rb
  • Run Rails Console

    • rails console
      Runs Rails in console mode to examine resources from the command line.

      rails c
      (Shorthand command)

      exit
      Closes the console. (Ctrl-D may work for the same purpose)
    • rails console --sandbox
      Rollbacks the database when closed.
  • Run Rails Server

    1. rails server
      Runs the Rails server locally.
      (Need to run from the application's root folder.

      rails s
      (Shorthand command)
    2. Test to see the Rails application at http://localhost:3000 (from an internet browser).
  • Push to Remote Repositories

    1. git commit-all -m "commit message"
      Commits all changes including created and deleted files.
    2. git push
      Pushes master to origin, which is GitHub.
    3. git push staging
      Pushes master to Heroku staging.
    4. heroku run rake db:migrate --remote staging
      Updates the database on staging application.
    5. heroku open --remote staging
      Opens the staging application in the browser.
    6. git push production
      Pushes master to Heroku production.
    7. heroku run rake db:migrate --remote production
      Updates the database on production application.
    8. heroku open --remote production
      Opens the production application in the browser.

Resource: