Build Rails API-only application with serializers
✓Works with OpenClaudeYou are a Rails backend developer building RESTful API-only applications with proper serialization layers.
What to check first
- Run
rails --versionto confirm Rails 6+ is installed (API-only mode is standard) - Verify
Gemfileincludesactive_model_serializersgem or uses built-inrender json:with serializer classes - Check if the Rails app was generated with
rails new myapp --apiflag (skips view/asset pipeline)
Steps
- Generate a new API-only Rails app with
rails new myapp --api --database=postgresqlto skip unnecessary middleware - Add
gem 'active_model_serializers'to Gemfile and runbundle install - Generate a resource scaffold with
rails generate scaffold Post title:string content:text user:references --apito create model, controller, and migration - Create serializer files manually or via
rails generate serializer Postinapp/serializers/post_serializer.rb - Define serializer attributes and associations using
attributes :id, :title, :contentandbelongs_to :user, serializer: UserSerializer - Update controller actions to render with serializers:
render json: @post, serializer: PostSerializeror let Rails infer automatically - Test JSON response structure with
curl -H "Content-Type: application/json" http://localhost:3000/api/v1/posts - Add pagination with
gem 'kaminari'and render paginated results:render json: @posts, each_serializer: PostSerializer, meta: pagination_meta(@posts)
Code
# config/routes.rb
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :posts do
resources :comments, only: [:index, :create]
end
resources :users
end
end
end
# app/models/post.rb
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
validates :title, :content, presence: true
end
# app/serializers/post_serializer.rb
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :content, :created_at, :updated_at
belongs_to :user, serializer: UserSerializer
has_many :comments, serializer: CommentSerializer
end
# app/serializers/user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
end
# app/controllers/api/v1/posts_controller.rb
module Api
module V1
class PostsController < ApplicationController
before_action :set_post, only: [:show, :update, :destroy]
def index
@posts = Post.all.page(params[:page]).per(10)
render json: @posts, each_serializer: PostSerializer
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Ruby on Rails Skills
Other Claude Code skills in the same category — free to download.
Rails Setup
Scaffold Ruby on Rails app with models and controllers
Rails Active Record
Write Active Record models with associations and validations
Rails Testing
Write RSpec tests with factories and mocks
Rails Stimulus
Build interactive UIs with Hotwire and Stimulus
Rails Sidekiq
Set up background jobs with Sidekiq
Rails ActiveRecord Performance Optimization
Fix N+1 queries, slow scopes, and ActiveRecord pitfalls in production Rails apps
Rails Action Cable WebSockets
Build real-time features in Rails using Action Cable WebSockets
Want a Ruby on Rails skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.