Providing Rails JSON:API boilerplate
Here is a concise Ruby on Rails API boilerplate using jsonapi-serializer that supports compound documents with sideloaded related resources via ?include=author,comments for a Post resource:
# Gemfile
gem 'jsonapi-serializer'
# Run bundle install:
# bundle install
# config/routes.rb
Rails.application.routes.draw do
resources :posts
end
# app/models/author.rb
class Author < ApplicationRecord
has_many :posts
end
# app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :post
end
# app/models/post.rb
class Post < ApplicationRecord
belongs_to :author
has_many :comments
end
# app/serializers/author_serializer.rb
class AuthorSerializer
include JSONAPI::Serializer
attributes :name, :email
end
# app/serializers/comment_serializer.rb
class CommentSerializer
include JSONAPI::Serializer
attributes :body, :created_at
end
# app/serializers/post_serializer.rb
class PostSerializer
include JSONAPI::Serializer
attributes :title, :content, :created_at
belongs_to :author
has_many :comments
end
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
posts = Post.all
render json: PostSerializer.new(posts, include: params[:include]&.split(',')).serialized_json
end
def show
post = Post.find(params[:id])
render json: PostSerializer.new(post, include: params[:include]&.split(',')).serialized_json
end
end
This enables compound documents where clients can request related author and comments with URLs like /posts/1?include=author,comments, minimizing HTTP requests per JSON:API specs.
Below is a minimal Rails API boilerplate demonstrating JSON:API–compliant responses using the jsonapi-serializer gem. It includes compound documents (sideloading) for author and comments via the ?include= query param.
1. Gemfile
# Gemfile
source 'https://rubygems.org'
ruby '3.2.2' # adjust to your Ruby version
gem 'rails', '~> 7.0'
gem 'pg' # or any DB you use
gem 'puma'
gem 'jsonapi-serializer'
Then run:
bundle install
rails new myapp --api
(If you created a new Rails app first, just add jsonapi-serializer to the Gemfile and run bundle install.)
2. Models (example)
# app/models/author.rb
class Author < ApplicationRecord
has_many :posts, dependent: :destroy
end
# app/models/post.rb
class Post < ApplicationRecord
belongs_to :author
has_many :comments, dependent: :destroy
end
# app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :post
end
3. Serializers (jsonapi-serializer)
# app/serializers/author_serializer.rb
class AuthorSerializer
include JSONAPI::Serializer
set_type :author
attributes :name, :email
has_many :posts
end
# app/serializers/comment_serializer.rb
class CommentSerializer
include JSONAPI::Serializer
set_type :comment
attributes :body, :created_at, :updated_at
belongs_to :post
end
# app/serializers/post_serializer.rb
class PostSerializer
include JSONAPI::Serializer
set_type :post
attributes :title, :content, :created_at, :updated_at
belongs_to :author
has_many :comments
end
4. Controllers
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
# GET /posts?include=author,comments
def index
posts = Post.includes(:author, :comments).all
render json: PostSerializer.new(
posts,
include: parse_includes
).serialized_json
end
# GET /posts/:id?include=author,comments
def show
post = Post.includes(:author, :comments).find(params[:id])
render json: PostSerializer.new(
post,
include: parse_includes
).serialized_json
end
private
# Parse the include param into an array of relationships.
# For example: ?include=author,comments => [:author, :comments]
def parse_includes
return [] unless params[:include].present?
params[:include].split(',').map(&:strip).map(&:to_sym)
end
end
(Add similar controllers for authors and comments as needed.)
5. Routes
# config/routes.rb
Rails.application.routes.draw do
resources :posts, only: [:index, :show]
# resources :authors, :comments if you need them
end
6. Example Requests
-
List Posts with sideloaded author and comments:
curl "http://localhost:3000/posts?include=author,comments"
This returns a JSON:API compound document containing data for posts plus an included section for author and comments.
-
Show a Single Post with sideloaded author and comments:
curl "http://localhost:3000/posts/1?include=author,comments"
The responses will follow JSON:API conventions, including type, id, attributes, relationships, and (when requested) the included array with related resources.