RoR

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

  • As of Mac Os X Leopard RoR is closely integrated:
  • Ruby and [RoR|http://rubyonrails.org/]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|http://macromates.com/]\- clever editor that grew up around RoR. Textmate has a subversion plugin. See session 426 on creating a rails application under Snow Leopard.

[RoR|http://rubyonrails.org/] uses [YAML|http://en.wikipedia.org/wiki/YAML] to specify structured data.

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

A MVC framework (Model, View Controller).

[Active Record|http://api.rubyonrails.org/?u=ar.rubyonrails.com]: 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|http://api.rubyonrails.org/?u=ar.rubyonrails.com] : the Controller class that requests data from the [Active Record|http://api.rubyonrails.org/?u=ar.rubyonrails.com] (model).

[Action View|http://www.devarticles.com/c/a/Ruby-on-Rails/Understanding-Action-Views-in-Ruby-on-Rails/]: The ActionView helps you present data to your users. Note that a .erb file is basically HTML with embedded ruby code.

[Domain Specific Language|http://en.wikipedia.org/wiki/Domain-specific_language]: A optimised vocabulary for a specific purpose ('jargon'). Think of Active Record:

class Post < ActiveRecord::Base
  has_many :comments
  ...
end

class Comment < ActiveRecord::Base
  belongs_to :post
  ...
end

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

Author.find_by_first_name "George"

[hpricot|http://www.robbyonrails.com/articles/2007/02/13/get-to-know-a-gem-hpricot]: A useful Ruby plugin to both validate and parse HTML - [hpricot testing|http://lukeredpath.co.uk/blog/testing-your-rails-views-with-hpricot.html]. Also does XML parsing - e.g. take XML data set and import.

Testing

[RoR|http://rubyonrails.org/] 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|http://rubyonrails.org/] uses [YAML|http://en.wikipedia.org/wiki/YAML] to specify structured data in testing. A test data file could look like:

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

steve:
 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 |http://en.wikipedia.org/wiki/Ruby_on_Rails]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|http://www.akitaonrails.com/2008/5/25/rolling-with-rails-2-1-the-first-full-tutorial-part-1] has a [named_scope|http://media.railscasts.com/videos/108_named_scope.mov] method that makes performing finds on models very elegant and convenient. This [named_scope|http://media.railscasts.com/videos/108_named_scope.mov] 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|http://www.json.org/]. RoR deals with this by having a [respond_to|http://info.michael-simons.eu/2007/08/06/rails-respond_to-method/] method.

Deployment

RoR is easy to deploy onto Mac OS X as [Mongrel|http://mongrel.rubyforge.org/] comes pre-installed. Mongrel is a web server that knows about Rails. [Passenger |http://weblog.rubyonrails.org/2008/4/11/passenger-mod_rails-for-apache-launches] is a a Rails deployment tool available on OS X. To use [Passenger|http://weblog.rubyonrails.org/2008/4/11/passenger-mod_rails-for-apache-launches] you do need to install an Apache module.

Scheduling data inputs -

Use [Launchd|http://en.wikipedia.org/wiki/Launchd] rather than cron. You can use [lingon|http://sourceforge.net/projects/lingon/] to edit and create [Launchd|http://en.wikipedia.org/wiki/Launchd] configuration files in Mac OS X Leopard. Remember to set the [RAILS_ENV|http://glu.ttono.us/articles/2005/09/05/environments-in-rails-0-13-1] variable (dev, test, production).

Useful links

[Pragmatic programming guide|http://www.pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition]

[Rails Development on OS X Leopard|http://developer.apple.com/tools/developonrailsleopard.html]

Ruby on windows: include tab completion

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

To tab complete:

"Hello".rev<TAB> or perhaps
Hello".rev<TAB><TAB>

will give you possible methods. To see array keys:

rb> dvds.keys

to see array values:

irb>dvds.values

Useful links on running rails jobs in cron

http://www.ameravant.com/posts/recurring-tasks-in-ruby-on-rails-using-runner-and-cron-jobs
http://www.myersds.com/notebook/2007/01/31/automation_with_rails_script_runner

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  + "'"

try:

  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:
http://localhost:8808/

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

gem install <path_to_gemfile>

Note:

rake -T
rake routes
ruby script/dbconsole

Note:

filter_parameter_logging :password in application_controller.rb for security.

Note:

<%= 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"}
                    /:controller/:action/:id
                    /:controller/:action/:id(.:format)

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

Cucumber

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