Service Rendering
The Problem
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
Implementation
Basic Helper
Usage
Advanced Implementation
With Custom Response Building
Usage with Options
With Serializers
Handling Different Error Types
What's Next?
Last updated