Why use RoR (Ruby on Rails)? (19-03-09)*

  • As of Mac Os X Leopard RoR is closely integrated:
  • Ruby and [RoR|]are pre-installed in Leopard
  • SQLite is bundled - useful for developing prototype apps.
  • iPhone simulator means can test app for an iPhone.
  • MySql pre-bundled for production version.
  • RoR management tool built in.

More about RoR (20_03_09)

[Textmate|]\- clever editor that grew up around RoR. Textmate has a subversion plugin. See session 426 on creating a rails application under Snow Leopard.

[RoR|] uses [YAML|] to specify structured data.

[RoR|] has built-in Unit testing so that the application can use a 'test driven specifiation'.

A MVC framework (Model, View Controller).

[Active Record|]: In Rails, the *model* is handled by what's called an object-relational mapping layer entitled Active Record. This layer allows you to present the data from database rows as objects and embellish these data objects with business logic methods. In general one active record class relates to one DB table.

[Action Controller|] : the Controller class that requests data from the [Active Record|] (model).

[Action View|]: The ActionView helps you present data to your users. Note that a .erb file is basically HTML with embedded ruby code.

[Domain Specific Language|]: A optimised vocabulary for a specific purpose ('jargon'). Think of Active Record:

class Post < ActiveRecord::Base
  has_many :comments

class Comment < ActiveRecord::Base
  belongs_to :post

Say you wanted to find an author named 'George':

Author.find_by_first_name "George"

[hpricot|]: A useful Ruby plugin to both validate and parse HTML - [hpricot testing|]. Also does XML parsing - e.g. take XML data set and import.


[RoR|] has built-in Unit testing so that the application can use a 'test driven specifiation'. In other words you design the test before you design the code. Test skeletons are built automatically. [RoR|] uses [YAML|] to specify structured data in testing. A test data file could look like:

# low  behold!  I am a YAML comment!
 id: 1
 name: David Heinemeier Hansson
 birthday: 1979-10-15
 profession: Systems development

 id: 2
 name: Steve Ross Kellock
 birthday: 1974-09-27
 profession: guy with keyboard

Note how indentation separates a test case (e.g. david, steve above) from the actual data (indented).

The DRY principle:

[RoR |]simplifies code by using DRY: Do not repeat yourself. "Don't repeat yourself" means that information is located in a single, unambiguous place. For example, using the ActiveRecord module of Rails, the developer does not need to specify database column names in class definitions. Instead, Ruby on Rails can retrieve this information from the database. More code == more mistakes.

Named Scope

[Rails 2.1|] has a [named_scope|] method that makes performing finds on models very elegant and convenient. This [named_scope|] allows you to chain find methods together. Named scoped can also used lamdba functions (function without a name that is evaluated at run-time).

Same data, different formats

E.g. HTML and [JSON|]. RoR deals with this by having a [respond_to|] method.


RoR is easy to deploy onto Mac OS X as [Mongrel|] comes pre-installed. Mongrel is a web server that knows about Rails. [Passenger |] is a a Rails deployment tool available on OS X. To use [Passenger|] you do need to install an Apache module.

Scheduling data inputs -

Use [Launchd|] rather than cron. You can use [lingon|] to edit and create [Launchd|] configuration files in Mac OS X Leopard. Remember to set the [RAILS_ENV|] variable (dev, test, production).

Useful links

[Pragmatic programming guide|]

[Rails Development on OS X Leopard|]

Ruby on windows: include tab completion

C:\Documents and Settings\student>irb -r irb/completion

To tab complete:

"Hello".rev<TAB> or perhaps

will give you possible methods. To see array keys:

rb> dvds.keys

to see array values:


Useful links on running rails jobs in cron

Including global declarations in separate config files:

Copy File.join declaration from the top of environment.rb- e.g.

require File.join(File.dirname(__FILE__), 'olm'

Session[:name] problem

Where you have:

session[:name]  = "'" + @current_user[0]['displayname'][0].to_s  + "'"


  1. Removing to_s
  2. Replacing the first "'" with ""
  3. Replacing/removing the second "'" with ""

To see all rails gems:

gem list --remote

To see rails docs:

C:\Documents and Settings\student\test>gem server
Then goto:

N.B. Can download .gem file directly then:

gem install <path_to_gemfile>


rake -T
rake routes
ruby script/dbconsole


filter_parameter_logging :password in application_controller.rb for security.


<%= link_to 'Edit', edit_post_path(post) %>
which creates a path as opposed to:
<%= link_to 'Edit', edit_post_url(post) %>
which creates a URL although browser may convert path to a URL.

Rails helpers

Code in helpers is available to both controller and view.

Default gem source

The default gem source is rubyforge

Rake routes

Note HTTP verbs (uppercase) and rails default verbs (:action)

C:\Documents and Settings\student\rails\blog>rake routes
(in C:/Documents and Settings/student/rails/blog)
       posts GET    /posts(.:format)                   {:action=>"index", :controller=>"posts"}
             POST   /posts(.:format)                   {:action=>"create", :controller=>"posts"}
    new_post GET    /posts/new(.:format)               {:action=>"new", :controller=>"posts"}
   edit_post GET    /posts/:id/edit(.:format)          {:action=>"edit", :controller=>"posts"}
        post GET    /posts/:id(.:format)               {:action=>"show", :controller=>"posts"}
             PUT    /posts/:id(.:format)               {:action=>"update", :controller=>"posts"}
             DELETE /posts/:id(.:format)               {:action=>"destroy", :controller=>"posts"}
    comments GET    /comments(.:format)                {:action=>"index", :controller=>"comments"}
             POST   /comments(.:format)                {:action=>"create", :controller=>"comments"}
 new_comment GET    /comments/new(.:format)            {:action=>"new", :controller=>"comments"}
edit_comment GET    /comments/:id/edit(.:format)       {:action=>"edit", :controller=>"comments"}
     comment GET    /comments/:id(.:format)            {:action=>"show", :controller=>"comments"}
             PUT    /comments/:id(.:format)            {:action=>"update", :controller=>"comments"}
             DELETE /comments/:id(.:format)            {:action=>"destroy", :controller=>"comments"}

Useful links:

Railsforphp: [railsforphp]

Useful commands

rake routes
rake -T 
ruby script/dbconsole
rake db:test:clone
rake db:drop:all
rake -T db
script/generate migration
cucumber features -t @seo
ruby script/generate mailer UserMailer


Miising 'a' in console:

dos> chcp 1252

Undefined snippets/steps:
Cut and paste from the cucumber output - brown.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License