Learn to build a Rails app using Heroku and Git

In 2009, I discovered Heroku service for building applications. Not long time ago was bought by Salesforce and it has expanded its list of programming languages including Java, Scala, Clojure and Node.js. It is extremely useful and extremely easy to use. It is also based in Git a SCM system which is also revolutionazing the version control systems.

To install Git, we could use this guide located on the Pro Git page or also you can see this page which explains how to use textmate with git

There is a great Quickstart for using Heroku which I used as a base for this post: http://docs.heroku.com/quickstart . It explains how to:

Sign in in Heroku

Install Heroku

Install RVM, Gemsets, Rails, Heroku and Postgres SQL Database

Heroku has improved a lot its installation procedures and it provides now a packaged installer called Heroku Toolbelt. The Heroku Toolbelt runs over Ruby 1.9, since my Mac OS X Lion still use Ruby 1.8.7, it is recommended that we install the Ruby Version Manager.

Install the Ruby Version Manager

The Ruby Version Manager is a command-line tool that makes it easy to install and manage multiple independent Ruby environments on the same machine. The Ruby Version Manager compiles the Ruby from the source. We could install RVM from our MacOSX using the Ruby installed with the following command

To start using RVM you need to run source /Users//.rvm/scripts/rvm. We check if it is correctly installed, running the following command:

user$ rvm --versionuser$ rvm

user$ rvm --version
user$ rvm

Once installed, we display a list of all the existing “known” Rubies using the following commands:

user$ rvm list known

user$ rvm list known

Heroku’s newest stack, “Celadon Cedar,” supports Ruby 1.9.3 and Rails 3.2. As of 9 May 2012, Heroku officially supports Ruby 1.9.3. To install a new version, we execute the following command:

user$ rvm install 1.9.3

user$ rvm install 1.9.3

This script will download, compile, and install the Ruby 1.9.3 into a directory managed by RVM (it’s under the ~/.rvm directory) different from the version installed in Mac OS X. To use this distribution, we execute the following command:

Generate RVM Gemsets to create independent Rails installations

As we said before, RVM gives you independent ruby setups. This means that ruby, gems and irb are all separate and self-contained – from the system, and from each other. You may even have separate named gem sets. We could list the existing gemsets available in the application with the following command:

Create a Rails Application

Rails App

Next, open your Gemfile and add the following lines:
group :production do
gem “pg”
end

# go into the new project directory and create an .rvmrc for the gemset
$ cd
$ rvm –rvmrc 1.9.3@awesome_rails_project

Access to the rails app: cd myapp

h3. Create first Heroku Entry

Execute git: $ git init

Do git add: $ git add .

We access to the following command:

Do git first commit: $ git commit -m "new app"

We open the application, and we start the local server ruby script/server and access to localserver using http://127.0.0.1:3000

As usual, we generate a index or home controller. I generally call them dashboard as it is sexy: $ ruby script/generate controller dashboard index

We delete the entry page: $ rm public/index.html

We modify the config/routes.rb to enter the following route map:

We create the heroku application directly on the heroku site from our computer merely using the Heroku’s gem

Now the web site will be visible on the following address: http://severe-stone-45.heroku.com. Sustitute the name by the name provided by Heroku and you got it. As the heroku repository is blank when we create the application we need to deploy the code to a specific branch, we deploy the code throuh this instruction