Goal
You’ll be able to use partials and layouts to avoid duplicating view code, and understand specifically when a partial is the right tool versus when a layout is.
Learn
A partial is a reusable chunk of view template, extracted so it can be rendered from multiple places without duplicating the same HTML:
# app/views/products/_product.html.erb (note the underscore prefix) <div class="product"> <h3><%= product.name %></h3> <p><%= product.price %></p> </div> # In another view: <%= render partial: "product", collection: @products %>
The leading underscore in the filename is a Rails convention specifically identifying it as a partial (not a full standalone view); it’s omitted when referencing it in render. Using collection: automatically renders the partial once per item, without needing an explicit loop.
A layout is a different, broader wrapper — the shared HTML structure (like the overall page shell, navigation, footer) that wraps around every individual view’s content:
# app/views/layouts/application.html.erb
<html>
<body>
<%= yield %> <!-- the specific view's content gets inserted here -->
</body>
</html> The genuinely important distinction: a partial is for reusing a piece of content across multiple different views (like a single product card shown on both an index and a search results page); a layout is for wrapping every view with the same overall page structure. Confusing the two, or trying to use one where the other genuinely fits better, usually leads to either excessive duplication or an unnecessarily rigid, hard-to-reuse structure.
Decision Task
You need the exact same “product card” HTML to appear both on the products index page and on a separate “featured products” page. Before reading on: is this a partial situation or a layout situation, and why?
Show Answer
A partial — this is specifically about reusing one piece of content (a single product card) across multiple different views, exactly what partials are designed for. A layout would be the wrong tool here, since layouts wrap an entire page’s structure, not a specific reusable content fragment appearing on some pages but not others.
Common Mistake
Duplicating the same chunk of view HTML across multiple template files instead of extracting it into a partial. Beyond the repetition itself, this creates a genuine maintenance problem: a future design change to that content requires finding and updating every duplicated copy individually, rather than editing one partial that every view already references.
Practice Questions
1. What filename convention identifies a partial in Rails?
Show Answer
A leading underscore, like _product.html.erb.
2. Write a render call that displays a partial called “comment” once for each item in a collection called @comments.
Show Answer
<%= render partial: "comment", collection: @comments %>
3. What does do inside a layout file?
Show Answer
Marks the specific point where the current view’s actual content gets inserted into the shared layout structure.
4. True or False: a layout and a partial serve essentially the same purpose, just with different syntax.
Show Answer
False — a partial reuses a piece of content across multiple views; a layout wraps every view with the same overall shared page structure, a genuinely different purpose.
5. Why is duplicating the same view HTML across multiple templates a genuine maintenance problem, not just a style preference?
Show Answer
A future design change to that content requires finding and updating every duplicated copy individually, rather than editing one shared partial that every view already references.
Try It Yourself
Without looking back, explain in your own words the core difference between when you’d reach for a partial versus when you’d reach for a layout.
Show Answer
A partial is for reusing a specific piece of content across multiple different views (like a product card appearing on several different pages); a layout is for wrapping every view with the same shared overall page structure (like the navigation and footer appearing on literally every page of the site).
Quick Check
1. What is a partial?
Show Answer
A reusable chunk of view template, extracted to avoid duplicating the same HTML across multiple views.
2. What filename convention identifies a partial?
Show Answer
A leading underscore.
3. What does a layout wrap?
Show Answer
The shared overall page structure around every individual view’s content.
4. What does mark inside a layout?
Show Answer
Where the current view’s specific content gets inserted.
5. When would you use collection: with render?
Show Answer
When rendering the same partial once per item in a collection, without writing an explicit loop.