Partial

A Partial in Rails is a reusable view template that allows developers to extract and share common UI components across multiple views.

Table of Contents

What is a Partial in Ruby on Rails?

A Partial is a smaller, reusable piece of a view that can be rendered inside other views, layouts, or partials. Instead of repeating the same markup in multiple places, developers can place it in a partial and reuse it wherever needed.

Partials help organize view code, reduce duplication, and make templates easier to maintain. They are commonly used for components such as forms, navigation menus, headers, footers, and lists of records.

In Rails, partial files are identified by a leading underscore (_) in their filenames.

In short, partials allow developers to write reusable view code and keep templates DRY (Don't Repeat Yourself).

Why are Partials Useful?

Duplicating view code can make applications harder to maintain and update. Partials are useful because they:

  • Reduce repetitive markup across views
  • Promote code reuse and consistency
  • Keep view templates smaller and easier to read
  • Simplify maintenance and updates
  • Encourage DRY principles in the presentation layer
  • Can accept local variables for flexibility

They help developers build modular and maintainable user interfaces.

How Partials Work?

A partial contains reusable view code and is rendered from another template when needed.

Key components:

  • Partial Files: Stored in the views directory with an underscore prefix
  • Render Method: Used to include a partial in a view
  • Locals: Pass data into a partial
  • Collections: Render multiple records using a single partial
  • Nested Partials: Partials can render other partials
  • The as: option: Rename the local variable when rendering a collection
  • The layout: option: Wrap a partial inside a layout template

Example

Scenario 1: Reusing a user information component Partial (app/views/users/_user.html.erb)

<div class="user-card"> 
 <h3><%= user.name %></h3> 
 <p><%= user.email %></p> 
</div> 

This partial displays a user's basic information.

Rendering the partial (app/views/users/index.html.erb)

<%= render partial: "user", locals: { user: @user } %>

Rendering the partial - Idiomatic shorthand (preferred in modern Rails):

<%= render "user", user: @user %>

Locals can be passed directly as keyword arguments, making the syntax cleaner and more concise.

Scenario 2: Rendering a collection of records Controller:

@users = User.all 

View (app/views/users/index.html.erb):

<%= render @users %> 

Rails automatically renders the _user.html.erb partial for each user in the collection.

Using the as: option to rename the local variable:

<%= render partial: "user", collection: @users, as: :member %> 

Inside _user.html.erb, the record is now available as member instead of user. This is useful when the partial variable name needs to differ from the model name.

Scenario 3: Wrapping a partial in a layout

<%= render partial: "user", layout: "user_box", locals: { user: @user } %> 

This renders _user.html.erb wrapped inside the _user_box.html.erb layout partial. Useful for applying a consistent wrapper (like a card or panel) around reusable components.

Where to Use Partials?

  • Reusable forms across create and edit pages
  • Navigation bars and menus
  • Headers and footers
  • Lists of records and cards
  • Shared UI components across multiple pages
  • Complex views that need better organization

In Summary

Partials in Ruby on Rails are reusable view templates that help eliminate duplication and improve code organization. By breaking views into smaller, reusable components, they make applications easier to maintain, update, and scale. Using idiomatic shorthand syntax, the as: option for collections, and the layout: option for wrapping components, developers can build flexible and well-structured view layers in their Rails applications.