Simple Steps to Building Your Own Rails API from Scratch
By: Khanittha Krajangjaem
Tags: Rails, API, Ruby on Rails, Rails Api, Backend Development

Searching for Public API that satisfies your needs can get frustrating sometimes. I was that person who spent hours searching for an API that includes data that I already have in mind and what I expect it to look like. Then this idea occurs.

If you know the how-to — why not use that knowledge and build it yourself? Why spend hours searching when you have that power to build one on your own?

Let’s babble no more. Because today, my friends, we are gonna be making our own Rails API from scratch — Let’s dive into it!

step 1: Creating a new Rails API-Only Application

First, make sure to cd into any folder you want this new application to exist or you can create a new directory by using a mkdir command, when ready run:

~ // ♥ > rails new stranger-things-api --api

By including an --api flag, Rails knows to limit and remove any other default features that are not necessary in an API application.

step 2: Implementing Model(s) and Migration(s)

Once the new application is created, cd into that application directory. It’s time to open up your code editor and from there start out with:

~ // ♥ > rails g resource character name img_url

“Resource Generator” is a great option if you are creating an API, it creates quite a bit of code for you including the important stuff like: Model file, Migration file, Controller file, Resources, etc.

Model(s) — In this example, to keep it simple there’s only one Model to work with, but depending on what you are expecting, there can be more but make sure to include the associations between them.

Character Model created by the Resource Generator — by including name parameter “character”

Migration(s) — No wonder “Resource Generator” also creates a Migration file for you by including name and img_url parameters, and with the default type of string this is the Migration:

Seeds — Now that we have Model and Migration set up, because this is an API, this step of seeding the database is the highlight of the show. Everything you put in this file will be the data you’ll grab from the API later on so make sure you include the data you want to see.

Migration is ready, Seeds are ready, run:

~ // ♥ > rails db:migrate

~ // ♥ > rails db:seed

Schema file after <rails db:migrate>

step 3: Controller and Additional Modifications

Controller — file has been created with class CharactersController that inherits from ApplicationController , all of these come from using “Resource Generator”. Since I want to be able to just see the characters data and can look up a specific character, I’d only need index and show actions:

By using only: and slice() I minimize what will get rendered and what information will be included in the API. This might not be necessary and you can just simply do:

render json: characters for index

for index render json: character for show

but the data will include created_at and updated_at keys which you might not use so there you have options.

If your version of API has more complex model structure, consider looking up Serializer along with Fast JSON API.

Modifying Some Code

CORS — When generating a new application using “Resource Generator”, Gemfile gets created with gem 'rack-cors' commented out, make sure to uncomment and run bundle install in order to implement CORS.

By default, the browser doesn't allow AJAX requests other than the current site you’re visiting, this is a web security. By using CORS (Cross-origin resource sharing) you’ll be able to specify who or what can access your endpoint on the server side. Part of your Gemfile and cors.rb should look something like this:

cors.rb can be found in config/initializers/cors.rb

Resources — Using Resource Generator is so convenient, it gives us a full resources call in the routes.rb file. But in this example we are not going to use all of them. Since it only needs index action and show action, we can go ahead and modify routes.rb a little it won’t hurt.