Service Rendering
This recipe provides a clean way to render service results and errors in your Rails controllers, reducing boilerplate and ensuring consistent API responses.
The Problem
Without a helper, controller actions become repetitive:
class PostsController < ApplicationController
def create
service = Post::Create.run(service_args(attributes: params[:post]))
if service.success?
render json: service.post, status: :created
else
render json: { errors: service.errors.to_h }, status: :unprocessable_entity
end
end
def update
service = Post::Update.run(service_args(record: @post, attributes: params[:post]))
if service.success?
render json: service.post
else
render json: { errors: service.errors.to_h }, status: :unprocessable_entity
end
end
# ... same pattern repeated for every action
endThe Solution
Create a render_service helper that handles success and failure automatically.
Implementation
Basic Helper
Add this to your ApplicationController:
Usage
Advanced Implementation
With Custom Response Building
Usage with Options
With Serializers
If you're using a serializer library (like Alba, Blueprinter, or ActiveModel::Serializers):
Handling Different Error Types
What's Next?
Learn how to integrate Pundit authorization with Operandi:
Last updated