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 new
command. 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 --api
flag which tells Rails to create an API only application.
Note: Don’t get confused with the application name rails-api-application
which is only the name of our project.
Notice the extra files and folders that the plain rails new
command will create. While when we added the api
flag 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.rb
and 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
endend
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:
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!