How to Create a Backend Rails Application as an API

Shaqqour
3 min readMar 5, 2021

In one of my previous blog posts, I explained how to create a full Rails application using the Scaffold generator. However, in many cases, you will need Rails to serve as your backend application that renders APIs to the frontend application. In this blog post, I will show you how to create a backend Rails application as an API.

Create a New Rails Application

As always we will start by creating a new Rails application using rails newcommand. Let’s run the following command in the terminal:

$ rails new rails-application

Running this command will create a full Rails application that would also serve as a frontend application. We can continue this way, or we could choose a better way that would create what we need exactly which is only a Rails backend API application — without frontend components. Let’s choose the better way.

First, we will go back and delete what we just did using this command:

$ rm -r rails-application

Let’s rerun the rails new command, but now by specifying that it is an API application. Run the following command in your terminal:

$ rails new rails-api-application --api

Notice here that we added the --apiflag which tells Rails to create an API only application.
Note: Don’t get confused with the application name rails-api-applicationwhich is only the name of our project.

Full rails application vs API only application

Notice the extra files and folders that the plain rails newcommand will create. While when we added the apiflag it creates only the backend structure for a rails application that renders APIs to the frontend.

Let’s create a model, perhaps movies, and try rendering movies API to the frontend.

Create a Resource

Let’s use the resource generator to create our model, migration, controller, and add a route to this resource. Run the following command in your terminal:

$ rails g resource Movie title:string director:string year:string

And then migrate the database changes to create the movie table by running the migrate command:

 $ rails db:migrate

Add Some Data

Inside your db/seeds.rb, create some data so you can test and render some movies APIs. You can create your own, or copy-paste these three movies:

movies = Movie.create([{ title: 'Star Wars', director: "George Lucas", year: "2008" }, { title: 'Lord of the Rings', director: "Peter Jackson", year: "2001" }, { title: 'E.T.', director: "Steven Spielberg", year: "1982" } ])

Run rails db:seed to seed your database table with these three movies.

Rendering Movies as APIs

Now that we have our movies, let’s try to render these movies as an API and check it out in the browser. We will be working on the controller now. Open app/controllers/movies_controller.rband let’s add an index action that would render all the movies.

// app/controllers/movies_controller.rbclass MoviesController < ApplicationControllerdef index
movies = Movie.all
render json: movies
end
end

Here, we are getting all the movies we have in the movies table, and then render a JSON object that represents these movies. If you open your browser and go to http://localhost:3000/movies, you should see something similar to this:

Movies API

That was how you create backend rails API application that renders a JSON object to be used later on in the frontend application. If you have anything you would like to add, please commit below!

--

--

Shaqqour

Full stack software engineer. Passionate about making people’s lives better and easier through programming. LinkedIn.com/in/shaqqour