Introduction
Ruby on Rails remains a leading framework in 2026 for building fast and maintainable RESTful APIs. Its convention over configuration approach lets you deliver features in hours instead of days. This intermediate tutorial guides you through creating an articles management API with token authentication, validations, and JSON serialization. You'll learn how to structure a Rails API-only project, use Active Model Serializers, and write robust tests with RSpec. Each step includes complete, executable code.
Prerequisites
- Ruby 3.3+ and Rails 8.0+
- Basic knowledge of Ruby and REST
- PostgreSQL installed
- API testing tool (Postman or curl)
Initialize the API Project
rails new blog_api --api -d postgresql
cd blog_api
bundle install
rails db:createThe --api flag generates a lightweight structure without views. PostgreSQL is used for production. The db:create command prepares the database immediately.
API Configuration
We will now add Active Model Serializers and configure CORS to allow requests from the frontend.
Add Required Gems
gem 'active_model_serializers', '~> 0.10.14'
gem 'rack-cors'
gem 'bcrypt'
group :development, :test do
gem 'rspec-rails'
endActive Model Serializers handles clean JSON serialization. rack-cors enables cross-origin requests. bcrypt is required for token hashing.
Create the Article Model
class Article < ApplicationRecord
validates :title, :content, presence: true
validates :title, length: { minimum: 3, maximum: 100 }
endThe model includes strict validations. These rules are automatically applied before any database save.
Generate the API Controller
module Api
module V1
class ArticlesController < ApplicationController
def index
articles = Article.all
render json: articles, each_serializer: ArticleSerializer
end
def create
article = Article.new(article_params)
if article.save
render json: article, serializer: ArticleSerializer, status: :created
else
render json: { errors: article.errors }, status: :unprocessable_entity
end
end
private
def article_params
params.require(:article).permit(:title, :content)
end
end
end
endThe controller follows REST conventions. Validation errors are returned with the correct HTTP 422 status code.
Define API Routes
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :articles, only: [:index, :create, :show]
end
end
endThe v1 namespace enables clean API versioning. Only the necessary actions are exposed.
Create the Serializer
class ArticleSerializer < ActiveModel::Serializer
attributes :id, :title, :content, :created_at
endThe serializer controls exactly which fields are exposed and prevents sensitive data leaks.
Best Practices
- Always version your API using namespaces
- Use serializers to control exposed data
- Validate parameters with strong parameters
- Write RSpec tests for every endpoint
- Enable request logging in production
Common Errors
- Forgetting to configure CORS (cross-origin errors)
- Exposing all model attributes without a serializer
- Not handling validation errors on the client side
- Using unversioned routes
Going Further
Explore our advanced Ruby on Rails courses at example.com to master JWT authentication, Sidekiq jobs, and Kubernetes deployments.