Learn to build a Rails app using Heroku and Git

Posted on Posted in Ruby, RubyonRails

Hi All:

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

We set Git configuration:

$ git config --global user.name "Your Name"
$ git config --global user.email youremail@example.com

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

user$ curl -L https://get.rvm.io | bash -s stable

It runs:

rvm 1.14.10 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]

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 --version
user$ rvm

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

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

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:

user$ rvm use 1.9.3
user$ Using /Users/gonzaloperezprim/.rvm/gems/ruby-1.9.3-p194

We check if it is working using the following command:

user$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

Or:

user$ which ruby
/Users/<userid>/.rvm/rubies/ruby-1.9.3-p194/bin/ruby

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:

user$ rvm gemset list
gemsets for ruby-1.9.3-p194 (found in /Users/<userid>/.rvm/gems/ruby-1.9.3-p194)
         global

There is a global gem set. This global gemset contains gems that will be used across multiple projects, then a project-specific gemset for each of our Rails applications.

user$ rvm gemset use global
Using ruby-1.9.3-p194 with gem set global
user$ gem list
 
*** LOCAL GEMS ***
 
bundler (1.1.5)
rake (0.9.2.2)
rubygems-bundler (1.0.3)
rvm (1.11.3.5)

Now we can create a new gem set:
We are going to create a gemset specifically for installing Rails 3.2 version.

user$ rvm gemset create rails3
'rails3' gemset created (/Users/<userid>/.rvm/gems/ruby-1.9.3-p194@rails3).
user$ rvm use 1.9.3@rails3
Using /Users/<userid>/.rvm/gems/ruby-1.9.3-p194 with gemset rails3

Install Rails Gem

To update rails, we update the RubyGems tool which is installed and then we install the Rails framework including dependencies but no including the documentation:

user$ gem update --system
user$ gem install rails -v 3.2.6 --include-dependencies --no-rdoc --no-ri

We check that the sqlite3 database is installed

sqlite3 -version
<pre lang="bash">user$ gem install sqlite3 --no-rdoc --no-ri

We are going to backup the gemset installed in the project, so we could export and then imported for keeping all the possible developers working in the same version.

user$ rvm gemset export rails.gems
Exporting current environments gemset to rails.gems
user$ cat rails.gems
# rails.gems generated gem export file. Note that any env variable settings will be missing. Append these after using a ';' field separator

Once we have export the rails.gems file, after we install the rvm, create the ruby installation and the gemset, we could

user$ rvm gemset import rails.gems

Install Heroku Gem

Now, we install Heroku gem

user$ gem install heroku --include-dependencies --no-rdoc --no-ri

We try to access using our Heroku registration details, the email and the password:

user$heroku login
Enter your Heroku credentials.
Email:   
Password (typing will be hidden):

Create a Rails Application

Rails App

Create the local application in your mac:

user$rails new myapp

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
$ rake db:create
db/development.sqlite3 already exists
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
# Enter the route to access to enter the dashboard
 map.root :controller =&gt; "dashboard"
rake db:migrate
heroku create
Created http://severe-stone-45.heroku.com/ | git@heroku.com:severe-stone-45.git

$ git push heroku master