Rails Features

This page is a list of some of the features of Rails. This is not complete, and I intend to add to it going forward as I come across things


Web pages often have common headers, footers, side rails, etc. This is done in a separate rhtml file. This file at some point has "<%= @content_for_layout %>". For any views within this controller, the content of the view will be placed at the @content_for_layout position.

Here is a sample layout from the Rails version of my MobileMusic sample application. It includes a menu bar along the top and a left rail.

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      	<%= stylesheet_link_tag 'main' %>
         <title><%= @page_title || 'MobileMusic' %></title>


      <div id="outer">
         <div id="header">
            <img src="/images/MobileMusicLogo.png"
                  alt="MobileMusic -- your home for music on the go!" />
            <br />
         	<div id="toolbar">
         		<%= link_to 'Home', :controller => 'welcome', :action => 'index' %>
         	  | <a class='toolbar_link' href='#'>Search</a>
         	  | <a class='toolbar_link' href='#'>My Account</a> 
         	  | <a class='toolbar_link' href='#'>FAQ</a>
         	  | <a class='toolbar_link' href='#'>Contact Us</a>
         <hr />
         <div id="left_rail">
            <span class="featured_artists">
               <h3>Featured Artists</h3>
               <em>Mark Growden</em><br />
               <strong>Live at the Odeon</strong><br />
               <img width="150" height="150" src="/images/markgrowden.jpg" /><br /><br />
               <em>Namely Us</em><br />
               <img width="150" height="150" src="/images/namelyus.jpg" /><br /><br />
         <div id="notices"><%= @flash[:notice] %></div>
      	<div id="body">
            <%= @content_for_layout %>


Note the title of this page. It defaults to 'MobileMusic', but it can be overridden if the controller overrides the @page_title field.


This is one of the biggest pieces, and I've talked it to death. ActiveRecord is the core to Rails. Without it, Rails would not offer much to entice people to use it. Rails would still be an obscure (though nice) framework in an obscure (though nice) language.

Oddly, this is the only area where Phobos does not seem to have copied Rails.

the Flash

The flash is a handy way to pass quick messages/warnings/etc. to the next page viewed. It is not a terribly complicated tool, but is amazingly handy. This has been well covered elsewhere, plus has an equivalent component in JSF.

No compiling

This is nice. With Ruby, you edit and the changes are visible instantly. Personally, I think the value of this is overrated. Given the loss in performance, I think it is a poor trade. However, a language like Rhino JS might be strong here. Unlike Java, it can be interpreted. Unlike Ruby, it can be compiled.

Rails Console

This is a repeat of what I wrote about the console for week 11, but I'm copying it here to keep everything in sync.

One neat feature of Rails is the console. This allows you to investigate your models and make changes on the fly. Here is an example where I add a new record to the songs table:

      $ ruby script/console 
      Loading development environment.
      >> s = Song.find_by_name "The Island"
      => #"42", "name"=>"The Island", "genre_id"=>"7", "album_id"=>"1", "duration_in_seconds"=>"120", "id"=>"3", "file"=>"namelyus-theisland.mp3"}>
      >> s.album.title
      => "Namely Us"
      >> song = Song.new
      => #0, "name"=>"", "genre_id"=>0, "album_id"=>0, "duration_in_seconds"=>0, "file"=>""}, @new_record=true>
      >> song.name = "Trouble" 
      => "Trouble"
      >> genre = Genre.find_by_name "Folk"
      => #"Folk", "id"=>"6"}>
      >> song.genre = genre
      => #"Folk", "id"=>"6"}>
      >> odeon = Album.find_by_title("Live at the Odeon")
      => #"2", "price"=>nil, "title"=>"Live at the Odeon", "url"=>nil, "id"=>"2"}>
      >> song.album = odeon
      => #"2", "price"=>nil, "title"=>"Live at the Odeon", "url"=>nil, "id"=>"2"}>
      >> song     
      => #0, "name"=>"Trouble", "genre_id"=>6, "album_id"=>2, "duration_in_seconds"=>0, "file"=>""}, @album=#"2", "price"=>nil, "title"=>"Live at the Odeon", "url"=>nil, "id"=>"2"}>, @genre=#"Folk", "id"=>"6"}>, @new_record=true>
      >> song.save
      => true
      >> quit

This can be a handy way to troubleshoot. Although it is mostly used for investigating models, it can also be used to load a session and investigate it.

And the kitchen sink

Next to ActiveRecord, if I had to list one key advantage of Rails over JSF and other Java web development frameworks, it would be that everything is included.

For a Java project, it would not be unusual to need:

Each one of these downloads takes time to set up and configure. For a new developer, this can be a daunting task. Even for an experienced developer, it might take a day to set up the application and all of the needed tools.

To get the equivalent functionality with Ruby on Rails, you need:

A new developer could get down to work within an hour.