Homework 11: DRY out with Partials

If you have been working through the Hartl tutorial, you probably already have created and incorporated partials.  If not, it is probably time to look through your views and do some refactoring to reduce redundancy.

Review your code, particularly any of your views (in /app/views) that may have sections of HTML which are repeated on all, or even several pages.  Most commonly, your header and footer (and possibly navigation bar, if it is separate from the header) are the same on every page of a website. If you have code that compensates for older browsers or checks to see if the page should be formatted for printing, these code fragments can also be removed from their appearance on every page and tucked into a common partial file that is rendered in views/layouts/application.html.erb.

Even if you have code that is common to several pages, you can pull it out of individual files to create a partial template and then have it file called where it is needed.  


  • Hartl 5.1.3 on partials
  • SaaS book section 5.1
  • Using Partials Edge Guide to Ruby on Rails

What to Do

  1. Review your views for code that is common to many, if not all, files. The Hartl tutorial can walk you through creating header and footer partial templates, if you haven’t done that already.  If you’ve done those steps while following along with the tutorial for other homework assignments, congratulations!! But also take a moment to review your files, looking for views that you could refactor.
  2. Create new partial templates, as appropriate, and call/render them from the original files … or the master layout file:  views/layouts/application.html.erb if they belong on every page (remember to cut out the duplicate code).
  3. Rerun your test suite and fix any errors you may have introduced

What to Hand In

  1. Similar to prior homework assignments,  submit a document that contains links to your GitHub repo (showing a new commit with “HW11” as part of the commit message)
  2. In the same document, include the link to the Heroku app. 
  3. Describe what code you refactored.  If you were ahead of the game and found nothing to DRY out, go ahead and tell me that.  

Scoring Rubric (7 points)

  1. Assignment includes link to the GitHub repo 
  2. Assignment includes link to the Heroku app. 
  3. Described what code, if any, was refactored.  
  4. Consistent header/navigation for the site
  5. Consistent footer for the site
  6. There are partials files in the views directory on the GitHub rep
  7. The partials are used in one or more pages
The views and opinions expressed on individual web pages are strictly those of their authors and are not official statements of Grinnell College. Copyright Statement.