Presenter
A Presenter is a design pattern used to move complex presentation logic out of views, keeping templates clean and easier to maintain.
Table of Contents
What is a Presenter in Ruby on Rails?
A Presenter is an object that sits between a model and a view, preparing data for display. Instead of placing formatting, conditional display logic, or UI-related calculations directly in views, developers can move that logic into a presenter.
Presenters are not built into Rails but are commonly used in Rails applications to improve code organization and separation of concerns. Some teams implement presenters as plain Ruby objects, while others use gems like Draper or ActiveDecorator to formalize the pattern.
A presenter typically wraps a model and exposes methods that return data in a format suitable for the user interface.
In short, presenters help keep views focused on rendering content while handling presentation-related logic elsewhere.
Why Are Presenters Useful?
As applications grow, views can become cluttered with formatting and conditional logic. Presenters help by:
- Keeping view templates clean and readable
- Separating presentation logic from business logic
- Reducing duplication across views
- Improving maintainability and testability
- Making complex UI formatting easier to manage
- Promoting better organization of application code
They are particularly helpful when the same display logic is used across multiple views.
How Do Presenters Work?
A presenter wraps a model and provides methods tailored for the view layer.
Key components:
- Presenter Class – Encapsulates presentation-related logic
- Wrapped Model – The model whose data is being presented
- Display Methods – Format data for rendering
- View Integration – Views call presenter methods instead of embedding logic
- Separation of Concerns – Keeps models and views focused on their primary responsibilities
Example
Scenario 1: Formatting User Information for Display Model(app/models/user.rb)
The User model is assumed to have first_name, last_name, and created_at attributes (the latter added automatically by Rails to every record).
class User < ApplicationRecord end
Presenter (app/presenters/user_presenter.rb)
class UserPresenter def initialize(user) @user = user end def full_name "#{@user.first_name} #{@user.last_name}" end def member_since @user.created_at.strftime("%B %d, %Y") end end
This presenter formats user-related data before it is displayed.
Controller
class UsersController < ApplicationController def show @user = User.find(params[:id]) @user_presenter = UserPresenter.new(@user) end end
View
<h2><%= @user_presenter.full_name %></h2> <p>Member since: <%= @user_presenter.member_since %></p>
The view remains simple while the presenter handles formatting logic.
Scenario 2: Displaying Account Status Presenter
class UserPresenter def initialize(user) @user = user end def account_status @user.active? ? "Active" : "Inactive" end end
View
<p>Status: <%= @user_presenter.account_status %></p>
Instead of placing conditional logic directly in the view, the presenter returns a display-friendly value.
These examples show how presenters help organize and reuse presentation logic.
Where to Use Presenters?
- Formatting user-facing data
- Displaying status labels and badges
- Complex conditional rendering
- Dashboard and reporting views
- Reusable UI presentation logic
- Applications with large or complex view templates
In Summary
A Presenter is a design pattern that moves presentation-related logic out of Rails views and into dedicated objects, improving code organization and the separation of concerns between models and views.