Ruby Newbie: Cool stuff this beginner is discovering about Rails (like partials)

I haven't stopped reading about and playing with with RoR after my Hands on with Ruby on Rails story, and I continue to uncover things that make Rails even more attractive as a time-saving Web development framework.

And if it seems like there's enough reader interest out there, I'll post my newbie discoveries from time to time.

Today's entry: Partials.

As the name implies, partials are snippets of code that reside in their own separate file, although they're not complete stand-alone code chunks. Partial file names always start with an underscore, so Rails knows they're partials.

Partials let you re-use small pieces of html display code again and again in an elegant and pain-free manner -- without having to worry that if you change something (such as the name of a variable), you have to go hunting down where that name occurs in a bazillion separate html file.

In the book Build Your Own Ruby on Rails Applications, author Patrick Lenz shows a rather cool example of how partials can make iterating over an array of search results more streamlined (and less annoying) than your typical for/next loop.

Say for example your Web application includes stories, and thus a story object. And, each story can get recommendations, or votes, from readers (Kind of like we have here at Computerworld.com -- in fact, if you'd like to try that out, do feel free to recommend my Ruby on Rails story :-) Ah, but I digress. ...). So you also have a vote object.

Now, say you want to create a box on your story page to show when each vote was cast for a particular story. After coding the query to pull that information from your database, how do you iterate over the results and display them?

Without partials, you could iterate over an array of results called "@storyvotes" by doing a for loop. This would require embedded Ruby in an HTML document looking something like this:

<ul>

<% for @vote in @storyvotes %>

<li><%= @vote.created_at %></li>

<% end %>

</ul>

Instead, using partials, you could first create an html view template that included this code

<ul>

<%= render :partial => 'vote', #the name of the partial filename, without the underscore

           :collection => @story.votes # the word "collection" shows there's more than one result %>

</ul>

In the partial file named _vote.rhtml you can include the code

<li><%= vote.created_at %></li>

and you're done. A partial file always has access to an object with the same name as the file name. That object is "automatically set to the current element of the collection that's being rendered," Lenz explains.

It may not seem like a big savings, but even after coding just a few small apps with a lot of repetitive display lists, this looks rather appealing. Plus, if you need to change your object name, or your logic, it's completely separate from your display code. And, if you or someone else need to come back to the code six months later to make changes, it would be relatively easy to find which part of the code is doing what, and know where it resides, because of the Rails file naming conventions.

Copyright © 2007 IDG Communications, Inc.

  
Shop Tech Products at Amazon