How to make REST API in Rails | Part 1

 How to make REST API in Rails

Making a simple API in rails

This is the first part of the tutorial series on "How to make ReST API in Rails". 
There are certain prerequisite which I assume that you had already installed. 
Here is the screencast for this tutorial.
 
 
prerequisites:
Ruby
Rails
Postgresql 

Create the app


$ rails new railsapi -d postgresql --skip-bundle

 
This will create the bare-bones scaffold of the rails application "railsapi" specifying
postgresql to use as database and do not install the gems while making the app.
 
In the Gemfile add gem to serialize the resources we are going to expose on the api

$ gem 'active_model_serializers' 

Also comment the 'jbuilder' and 'turbolinks' gem in the Gemfile, as we are not going
to deal with the frontend part. We will only create the json response which doesn't need these gems. 
Now go inside railsapi directory and install the gems
 
$ cd railsapi
$ bundle install

Making the API

To make api, we will isolate the api controllers under a separate namespace "api" by making a directory 
"api" in "app/controller" directory.
 
$ mkdir app/controllers/api
 
Now we will add this namespace in our routes.rb file
 
Rails.application.routes.draw do 
namespace :api do

end
end
 
Since we want our responses to be in json, So will set the default format of this api
to json. Change the above code.

Rails.application.routes.draw do 
namespace :api, defaults:{ format: :json }do

end
end
 
For now, our endpoint url should look like
http://localhost:3000/api/ 

Version the API

We should version our API from the beginning, so that it gives a better structure to our
rails app and in future if we need to add new feature our old code may keep on serving
the older feature while the new version will start serving new feature.

In order to set the version for the api, we first need to add another directory under the api directory
 
$ mkdir app/controllers/api/v1

Since we have another directory in api, we need to tell our routes.rb file to know
about this change. Let's modify routes.rb file

Rails.application.routes.draw do
# API routes path
namespace :api, defaults: { format: :json } do
namespace :v1 do

end
end
end

So we have added another namespace 'v1' inside api.
For now, our endpoint url should look like
http://localhost:3000/api/v1/

Adding a Model

Now let's add the User model using devise. To use devise add the Gem devise in Gemfile
 
gem "devise"
Now run bundle install to install devise gem. After doing bundle install we need to run the devise install generator  
 
$ rails g devise:install 
Let's generate the user model through the devise generator
 
$ rails g devise User
After doing these, let's now create database and run the migrations 
 
$ rake db:create
$ rake db :migrat

After installing devise the routes.rb should looks like this

Rails.application.routes.draw do
devise_for :users# API routes path
namespace :api, defaults: { format: :json } do
namespace :v1 do
#devise_for :users
resources :users, :only => [:show, :create]
end
end
end

Building first endpoint

Now we have the User model ready, we will create the first endpoint with the show action
which will return the user record in json format.
First generate users controller 
 
$ rails generate controller users

If the users_controller.rb file is not generated in api/v1/ directory than move the file
inside app/controllers/api/v1/
 
The users_controller.rb file should look like
 
class Api::V1::UsersController < ApplicationController
respond_to :json

def show
respond_with User.find(params[:id])
end
end

Here The 'A' of api and 'V' of v1 should be in caps. We have defined the response
format as json. The show action finds the user with the id passed as params and returns
the json representation of it.
 
For now, our endpoint url should look like
http://localhost:3000/api/v1/users/1
where 1 is the id
 
This will give an error now if you run the code as there is no record in the database.
So, Let's create one from rails console
 
$ rails console

> User.create({email: "[email protected]",password: "12345678",password_confirmation: "12345678"})
This will create a record with id 1. 
After this you can run rails server and check if our endpoint is working or not

$ rails s

In the new command line tab hit the endpoint to get the data

$ curl localhost:3000/api/v1/users/1
 
Output should be 
 

{"id":1,"email":"[email protected]","created_at":"2016-03-04T23:10:16.169Z","updated_at":"2016-03-04T23:10:16.169Z"}

If you are having problems with the response and have checked everything is well, then you
might need to visit the application_controller.rb file and update it as follows.
 

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :null_session
end

 

we should be using null_session to prevent CSFR attacks , so we should use it in order for POST or PUT requests to work 
 
By now you have learned how to create api with an endpoint. In the next post will learn
how to add more actions like create, update and delete/destroy to our api.
 
Next Chapter 

Further you can dockerize this Rails app - Follow this post to dockerize this API

18 thoughts on “How to make REST API in Rails | Part 1

Leave a Reply

Your email address will not be published. Required fields are marked *