Home arrow Ruby-on-Rails arrow Action Pack Partial Templates
RUBY-ON-RAILS

Action Pack Partial Templates


In this tenth part of a multi-part series on the Action Pack library for Rails, you'll learn what partials are and how they can save you a lot of time and effort. This article is excerpted from chapter six of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
May 19, 2011
TABLE OF CONTENTS:
  1. · Action Pack Partial Templates
  2. · Automatic Local Variable Assignment in Partials

print this article
SEARCH DEVARTICLES

Action Pack Partial Templates
(Page 1 of 2 )

Staying DRY with Partials

A typical web application is rife with view code and often suffers from a lot of needless duplication. Our HTML forms for adding and modifying users are good examples of forms that are very similar. Wouldnít it be nice if there was a way to reuse the common elements from one form in more than one place? Thatís where partial templates come in.

Partial templates, usually referred to as just partials, are similar to regular templates, but they have a more refined set of capabilities. Youíll see partials used quite often in a typical Rails application, because they help cut down on duplication and keep the code well organized. Partials follow the naming convention of being prefixed with an under score, thus distinguishing them from standard templates (which are meant to be rendered on their own.

Rather than creating two separate forms, weíll keep our code DRY by using a single partial and include it from both the new and edit templates. First, letís take the form code from new.rhtml and put that code in its own file, as shown in Listing 6-9. Weíll call this file _form.rhtml . Notice the leading underscore, which identifies it as a partial.

Listing 6-9. The app/views/users/_form.rhtml File

<%= error_messages_for :user %>

<p></p>

<p></p>

<p></p>

<p></p>

Now weíll revise the template in new.rhtml by including the partial, as shown in Listing 6-10, and do the same for edit.rhtml , as shown in Listing 6-11. Notice that when referencing the partial in the render method, you donít include the leading underscore.

Listing 6-10. The app/views/users/new.rhtml File

<h2>User Registration</h2>

<% form_tag :action => 'create' do %>

<%= render :partial => 'form' %>

<p><%= submit_tag 'Sign up!' %></p> <% end %>

Listing 6-11. The app/views/users/edit.rhtml File

<h2>Edit User</h2>

<% form_tag :action => 'update' do %>

<%= render :partial => 'form' %>

<p><%= submit_tag 'Save Changes' %></p> <% end %>

Letís take a closer look at the render method.

<%= render :partial => 'form' %>

A single argument is passed to render in the form of hash. (Are you noticing yet that Rails is a big fan of the options hash?) The symbol :partial is the key, and the partialís name is the value. Upon seeing this, the render method searches the current directory for a file named _form.rhtml . Notice that we donít need to include the leading underscore or the file extension when specifying the partialís name; Rails knows to look for a file in the same directory as the calling template with a leading underscore. Letís take a brief detour here to explain a few things about partials.

One of the things that makes partials unique is special convenience: automatic local variable assignment by way of convention over configuration. That was a mouthful. Allow us to explain.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials