Rails Shared Partials Templates
Rails shared partials are reusable ERB templates that live in a shared view folder, often app/views/shared. They are useful for repeated UI because they keep your Rails views DRY and readable for you and your LLMs.
Rails Blocks includes shared partial templates for common UI patterns. Copy the partial into your app, pass locals, and edit the markup like any other Rails view.
What are Rails shared partials?
A shared partial is an ERB file that starts with an underscore and can be rendered from multiple Rails views. Common locations are app/views/shared and app/views/shared/components.
Use partials when the same markup appears in several places and the interface can be expressed with a few locals.
Short definition
Rails shared partials are reusable ERB files for repeated view markup. They keep the markup in one place and accept local data from the calling view.
Why Rails Blocks for shared partials?
Rails Blocks gives you complete ERB partials for common UI components. This is useful when you want reusable Rails UI without adding a component framework or wrapping every template in a Ruby object.
Plain Rails ERB
Use normal Rails rendering, normal ERB, and normal locals. There is no extra abstraction to learn.
Fast adoption
Copy a partial into your app and render it from an existing view, layout, or Turbo frame.
Easy customization
Edit the Tailwind classes, change the markup, add locals, or split the partial when it gets too large.
Works alongside components
Use shared partials and Rails view components side by side, depending on what makes each piece of UI easiest to maintain.
A basic shared partial example
A Rails shared partial can accept locals for the content that changes between render calls:
<%# app/views/shared/components/_alert.html.erb %>
<div class="rounded-lg border p-4">
<p class="font-medium"><%= title %></p>
<p class="text-sm text-neutral-600"><%= message %></p>
</div>
Render the partial from any Rails view and pass the required locals:
<%= render "shared/components/alert",
title: "Saved",
message: "Your settings were updated." %>
When to use shared partials
- Use Rails shared partials when markup is repeated but still simple.
- Use them when a partial only needs a few locals.
- Use them when a Ruby class would make the UI harder to follow.
- Use Rails Blocks shared partial templates when you want a working ERB starting point.
Shared partials vs view components
Shared partials and view components are different ways to organize reusable Rails UI. A shared partial keeps the implementation close to ERB. A view component gives the UI a Ruby class and an explicit API.
Neither approach is the final form of the other. Use the one that makes the code easiest to read, change, and explain to another developer or LLM.
FAQ
Where should Rails shared partials live?
Most apps use app/views/shared. For UI components, app/views/shared/components keeps the folder easier to scan.
Are shared partials enough for a Rails design system?
Yes, they can be. Some teams build design systems with shared partials, some use view components, and many use both.
Do Rails Blocks partials work with Stimulus?
Yes. Partials can include Stimulus data attributes and use the same controllers as the component examples.