oreilly.comSafari Books Online.Conferences.


What Is Ruby on Rails
Pages: 1, 2, 3, 4, 5, 6, 7

Ajax & JavaScript helpers

Rails integrates the Prototype JavaScript library to implement its browser-side Ajax support, visual effects, and drag-and-drop abilities.

Rails has a simple, consistent model for how it implements Ajax operations. Once the browser has rendered and displayed the initial web page, different user actions cause it to display a new web page (like any traditional web app) or trigger an Ajax operation:

  1. A trigger action occurs. This could be the user clicking on a button or link, the user making changes to the data on a form or in a field, or just a periodic trigger (based on a timer).
  2. The client sends data associated with the trigger (a field or an entire form) asynchronously to an action handler on the server via XMLHttpRequest.
  3. The server-side action handler takes some action based on the data, and returns an HTML fragment as its response.
  4. The client-side JavaScript (created automatically by Rails) receives the HTML fragment and uses it to update a specified part of the current page's HTML, often the content of a <div> tag.

The real beauty is how easy Rails makes it to implement all of this in your web application. The following simple example adds new items to a list:

    <title>Ajax List Demo</title>
    <%= javascript_include_tag "prototype" %>
    <h3>Add to list using Ajax</h3>
    <%= form_remote_tag(:update => "my_list",
                           :url => { :action => :add_item },
                           :position => "top" ) %>
      New item text:
      <%= text_field_tag :newitem %>
      <%= submit_tag "Add item with Ajax" %>
    <%= end_form_tag %>
    <ul id="my_list">
      <li>Original item... please add more!</li>

The article Ajax on Rails and the JavaScript Helpers API explain more about Rails' Ajax support.

The Prototype library also provides the Rails developer with a wealth of browser-side visual effects. provides documentation and live demos of the Prototype library effects. Prototype also lets you easily add drag-and-drop features to your web application. also has live demos and documentation for Prototype's drag-and-drop support.


Layouts let you specify a common set of display elements for every page rendered by a controller. This is typically useful for common headers, footers, and sidebars. By default, Rails looks in its layouts directory for an rhtml file whose name matches the controller's name. A layout template might look like this:

    <title><%= @page_title %></title>
    <div>The header part of this layout</div>
    <div><%= @content_for_layout %></div>
    <div>The footer part of this layout</div>

Rails will substitute the HTML that an action renders into the above layout where it says @content_for_layout.

The controller can also directly specify the name of the layout template to use for all its actions. This makes it easy to use the same layout for multiple controllers. You can even dynamically choose a layout template at runtime. For example, you could use one layout for logged-in users and a different one for anonymous users.

To learn more about layouts, see the ActionController::Layout documentation.

Components and partials

Components and partials allow you to modularize your view templates.

The simplest are partials, which allow you to extract a common piece of a template into a separate file and then render it from many other templates (or many times within a single template). Partial templates always have a leading underscore on their filenames to distinguish them from full templates.

A typical use of a partial is for rendering a collections of items.

<% for ad in @advertisements %>
  <%= render :partial => "adview", :locals => { :item => ad } %>
<% end %>

This renders the partial template _adview.rhtml multiple times (once for each ad in the collection @advertisements). For each rendering, Rails will pass _adview.rhtml a local variable named item that contains the ad object to use.

The ActionView::Partials documentation explains more. Pointstorm has a more in-depth presentation of Ruby partials, but it is a little out of date because it uses the older, obsolete syntax for rendering partials.

Components are similar to partials in that they embed the rendering of another template within the current template. The difference is that you specify the name of a controller and action, and its template is the one to render and insert into the current template.

<%= render_component :controller => "calendar", :action => "today" %>

See Components in the Rails Manual to learn more.


Scaffolding allows you to get an instant implementation of CRUD (Create, Retrieve, Update, and Delete) operations on any database table. They're not pretty, but they do give you immediate web-based access to your tables. Over time, you can incrementally replace the generated CRUD operations and views with your own.

Rails supports static or dynamic scaffolding. Static scaffolding physically generates model, controller, and template files. This lets you see how it works and start tweaking this existing code. To generate static scaffolding, navigate in the file system to the root of your Rails web application and run a command similar to:

$ ruby script/generate scaffold invoices

This will look for an invoices table in your database and use its schema to generate the model, the controller, all of the view templates, the unit test skeletons, and more. The command displays a list of all the files that it generates.

The advantage of static scaffolding is that you get to see and modify the generated code. The disadvantage is that if you change you database table, the scaffolding will not reflect those changes. However, if you haven't modified the scaffolding, you can just regenerate it.

Dynamic scaffolding does not generate any files. Rails simply creates what it needs dynamically as your web app runs. This means that every time you change the database table, the scaffolding's CRUD views immediately show those changes. To request dynamic scaffolding, place a single line of code in the controller:

class AccountController < ActionController::Base
  scaffold :account

Much, much more

As with Active Record in the previous section, there is a lot more to Action Pack than I can cover here. To learn more, look at the Action Pack API.

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow

Sponsored by: