Build an API with Ruby on Rails

October 7, 2021
views 3 min read

In this article we'll learn how to build a simple API with Ruby on Rails. We're assuming you have RoR already installed.


Let us get started, open your favourite terminal application and run:

rails new our-api --api && cd our-api

By passing --api Rails will not generate any views in case we run a scaffold migration.

Great, now open our-api in your favourite text editor and navigate to config/routes.rb. We're about to set our first route, add the following:

Rails.application.routes.draw do
  get 'api/vehicles', to: 'application#vehicles'
end

Now open app/controllers/application_controller.rb and paste the following:

class ApplicationController < ActionController::API
  def vehicles
    render json: { name: 'Tesla Model 3' }
  end
end

Let's just test it out, see what happens. To start the server simply run:

rails s

Now if your browser supports rendering JSON just open http://localhost:3000/api/vehicles in your browser, otherwise I would suggest either using Postman or Insomnia.

The response should be following:

{
  "name": "Tesla Model 3"
}

Seed the database

We're using SQLite for the database, this means it's just a file within our project, no servers, no hosts. We can run Rails migrations to actually interact with the DB, on your terminal run:

rails g model Vehicle name:string price:decimal{10-2} picture:text

What does this means? It'll generate a model called Vehicle and 3 fields in the database, name, price and picture, go ahead and run:

rake db:migrate

Amazing, we can now take advantage of this, let us change our application_controller.rb:

class ApplicationController < ActionController::API
  def vehicles
    @vehicles = Vehicle.all

    render json: @vehicles
  end
end

If you hit our api/vehicles endpoint is should return an empty array [], that's normal, we still don't have any data on our database. Let's take care of that, open db/seeds.rb, you can see that there's already some examples there, but we can delete those and run our own:

Vehicle.create!([
  { name: 'Tesla Model S', price: '51885.17', picture: 'assets/model-s.png' },
  { name: 'Tesla Model 3', price: '100990', picture: 'assets/model-3.png' },
  { name: 'Tesla Model X', price: '120990', picture: 'assets/model-x.png' },
  { name: 'Tesla Model Y', price: '65000', picture: 'assets/model-y.png' }
])

Fun fact, the Tesla models actually spell the word Sexy (S3XY) 😮

rake db:seed

I bet if you now hit api/vehicles you'll see it's now returning 4 entries.


Bonus: Routes

Imagine you want to add more API resources, say locations, events and services, our routes.rb would look like this:

Rails.application.routes.draw do
  get 'api/vehicles', to: 'application#vehicles'
  get 'api/locations', to: 'application#locations'
  get 'api/events', to: 'application#events'
  get 'api/services', to: 'application#services'
end

We're repeating api/ a lot, we can scope our routes, such as:

Rails.application.routes.draw do
  scope :api do
    get 'vehicles', to: 'application#vehicles'
    get 'locations', to: 'application#locations'
    get 'events', to: 'application#events'
    get 'services', to: 'application#services'
  end
end

Hope you liked the article and helped you in any way. See you soon.

Read next
The Tale: Learning Javascript in 2 days
June 14, 2021
views