Rails API with FastJSON from Netflix

Photo by Tomas Anton Escobar on Unsplash

Project setup

First create a new rails project in api mode.

rails new fjsonrail --api --skip-tests -d mysql
gem 'faker', '~> 1.9', '>= 1.9.3'
gem 'fast_jsonapi'
gem 'kaminari', '~> 0.17.0'

Preparing some data

Create a model called Location, we will use these records for serialization.

rails g model Location name

Controllers

Api controller

We will introduce some helper methods to our API base controller. These helper methods will put pagination info when we have a collection of records for example.

Locations controller

With the help of the methods defined in application controller, our locations controller looks very simple.

Serializer

Creating a serializer is super easy. Just type:

rails g serializer Location name

That’s it

Don’t forget to update your routes.rb

Rails.application.routes.draw do
resources :locations, only: [:index, :show]
end

--

--

developer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store