Implementing RESTful API in Ruby on Rails

Implementing RESTful API in Ruby on Rails

30 Nov 2016
Implementing RESTful API in Ruby on Rails

What is RESTful API

A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST, and DELETE data. Representational state transfer (REST), which is used by browsers, can be thought of as the language of the Internet.


Steps in creating RESTful API: Step 1: Creating an API

While creating an API, the controller is kept in a separate folder named API inside the app/controller of the ROR application.


Step 2: Creating a Versioning Folder

Once an API folder is created, we create a versioning folder under the API folder. This versioning folder is created according to the API requirement. An existing API should never be modified, except for critical bug fixes. Rather than changing the existing API, we create another version of that API.

For ex., app/controller/api/v1 or app/controller/api/v2


Step 3: Creating a Controller File

Create a controller file inside the API folder like home_controller.rb


Step 4: Creating Controller Class

Inside the controller file, we need to create the controller class. This controller class will contain the method which is used while calling an API. Basically, the method inside the controller is nothing but a business logic of the requirement of an API. So, this method is defined to run as per the requirement of an API. This method will return the JSON format data. For that, we need to render the result into JSON format.

The above steps deal with the controller /API folder. Now, if someone wants to call a particular api method, for that we need to define routes.

The routes are used for calling API methods defined inside the config/ routes.rb file of the ROR application.


Route Example :

namespace :api do

resources :home, defaults: { format: 'json' } do

collection do

get 'business_details&:category_service&:city', to:'home#business_details'




So, our calling URL for the above step should be http://localhost:3000/api/home/business_details&Food andRestaurants&USA

And the result should be

{“name”: “MacDonalds”, “address”: “New York, USA”}



Jay singh


This one is very helpful for beginners in ror technology. thank and put some other helpful like this.


Leave a comment: