Continuing with my exploration of Ruby on Rails as a .NET guy, here’s an awesome example of Convention over Configuration at work. I decided to add bite size snippets of some of the cool things I encountered in Ruby and Rails. If you are interested in more, see my other Ruby posts.
Today’s post illustrates how elegantly the render partial helper method in Rails works. Assuming you’ve done some MVC development, you should be very familiar with a partial view. Partials are used to keep your view nice and clean. When things start getting complex within a view, you may want to think about adding a partial.
Take for example a library or bookstore application. One of the models in the application is, of course, a book. The books are displayed in a list as well as individually on a details page. During the course of development, you notice that you have quite a bit of repeated code between the index and show pages. This is a good opportunity to use a partial to DRY up your code. You take the repeated book view code (which I won’t post here since it doesn’t matter what it is), and put it into a partial (_book.html.erb). Within the partial we are referencing a local object simply called book.
On your detail page (a single book), it’s a simple replacement like so:
But even better, since our partial is named the same as our model, we can shorten up the syntax:
Now that’s some convention goodness right there.
Partials and Collections
The coolest thing is how we can use our partial on our index view (a listing of books). You may initially think to just iterate through the collection in a for loop, calling the partial on each pass. That certainly would work, but Rails gives us a nice and clean way to implement this instead:
And, you guessed it, you can actually shorten up this as well since our partial is named the same as the collection (well the singular version, “book” vs. “books”):
Isn’t that cool? But that’s not all that Rails gives us. Let’s say that within your partial template you decided to use a different local variable. Instead of calling our local variable in the partial “book,” we decided to call it
foo. How would you use the collection in that instance? Well it’s simple:
That tells Rails to take an object from the @books collection and reference it as foo for use within the template.
There is also a bonus counter variable that Rails provides when dealing with collections. The variable is named after the singular version of your collection followed by
_counter. In our
@books example, the variable would be called
book_counter. Accessing this variable will tell you how many times the partial has been rendered.
The last goodie that we get when using partials with collections is the option to use a separator or spacer in between each partial that is rendered. A spacer is simply another partial that will be rendered in between each pair of
_book partials, however, no data will be passed to it. To use the spacer, you simply supply a
:spacer_template to the
render method like so: