Partials and Layouts
Fast recall
Quick revision
Core answer
<%= yield %>. A partial is a reusable template fragment whose filename starts with _, such as _product.html.erb, and is rendered without that underscore. Layouts provide page-wide structure; partials compose smaller repeated pieces. Passing explicit locals gives a partial a clear input contract, and collection rendering can apply one partial efficiently to many records.Whether you know partials are _name files rendered with render 'name' (DRY repeated views) and layouts wrap the action with yield.
Naming a partial without the leading underscore (render looks for _form.html.erb), or forgetting yield in the layout (no action content shows).
Partials are _name.html.erb files, rendered with render 'name' (DRY repeated views). Layouts wrap the action with <%= yield %>. Compose pages from shared pieces.
Interview-ready guide
Interview answer
A Rails layout is the outer response template: it usually owns the document shell, shared navigation, notices, and footer. Its <%= yield %> marks where Rails inserts the selected action template. A partial is a smaller reusable fragment inside that page rather than a second full document.
Rails first renders the action's view content, including any partial calls, and places that result at the layout's yield point. A file named _product.html.erb is rendered without the underscore, for example render "product", product: @product. New and edit pages can similarly share one form partial while keeping their page headings separate.
Explicit locals show what a partial needs and avoid a hidden dependence on many controller instance variables. render "product", product: @product supplies a local named product. With render @products, Rails chooses the conventional product partial for each record and supplies that same local automatically.
Extract a partial for markup that repeats or represents a meaningful unit such as a card, form, or navigation section. Splitting every few lines into a file makes the response harder to follow. Partials also do not solve database access: load associations before rendering so a collection partial does not hide an N+1 query.
Partials and Layouts
Direct Answer
A Rails layout is the outer response template that wraps an action's view and inserts it at <%= yield %>. A partial is a reusable template fragment whose filename starts with _, such as _product.html.erb, and is rendered without that underscore. Layouts provide page-wide structure; partials compose smaller repeated pieces. Passing explicit locals gives a partial a clear input contract, and collection rendering can apply one partial efficiently to many records.
Deep Dive & Architecture
Rails first renders the chosen action template and its partial calls, then places that captured output into the layout's main yield location. Named content_for regions can supply additional values—such as a page title—to named yields in the same layout.
Partial lookup follows path and format conventions. A render call in a products view may find _product.html.erb in the current directory, while render 'shared/navigation' names a fragment in another directory. The partial's leading underscore distinguishes it from a full action template on disk.
Locals create a narrow data boundary. A partial receiving product: can be understood and tested without knowing which controller instance variables happen to exist. Collection rendering repeats that boundary for every object and can expose a counter when ordering information is needed.
Composition affects performance as well as organisation. Rendering a partial does not preload its associations, so a collection fragment that reads an unloaded relationship may trigger one query per row. Data needed by the view should be loaded before rendering, and fragment caching can be considered only after the data boundary is correct.
Code Example
<!-- app/views/layouts/application.html.erb -->
<html>
<head><title><%= content_for?(:title) ? yield(:title) : 'Shop' %></title></head>
<body><%= yield %></body>
</html>
<!-- app/views/products/index.html.erb -->
<% content_for :title, 'Products' %>
<h1>Products</h1>
<%= render @products %>
<!-- app/views/products/_product.html.erb -->
<article id="<%= dom_id(product) %>">
<h2><%= product.name %></h2>
</article>
Common Mistakes & Pitfalls
Naming a partial without the leading underscore (render looks for _form.html.erb), or forgetting yield in the layout (no action content shows).
Check your understanding
Answer these without looking back at the article.
- How does an action template reach the layout's `yield`?
- Why are partial locals often clearer than instance variables?
- How can collection partials reveal an N+1 query problem?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.