Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Learn how to build a modern API on Michael Hartl's Rails 5 tutorial
| Date | Stars |
|---|---|
| 2026-07-24 | 449 |
| 2026-07-25 | 449 |
| 2026-07-28 | 449 |
| 2026-07-30 | 449 |
| 2026-08-06 | 449 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
0.0
growth rate 0.00%/day
# Build an API in your Rails app now! (Rails 5 version)
_Note 1: If you are looking for the regular readme, it's [here](docs/README.md)._
_Note 2: You can contribute to this tutorial by opening an issue or even sending a pull request!_
_Note 3: With the API I built, I went on and created the [same app](https://github.com/vasilakisfil/ember_on_rails5) in Ember._
I will show how you can extend your Rails app and build an API without
changing a single line of code from your existing app.
We will be using [Michael Hartl's Rails tutorial](https://www.railstutorial.org/)
(I actually started learning Rails and subsequently Ruby from that tutorial, I really owe a
beer to that guy) which is a classic [Rails app](https://bitbucket.org/railstutorial/sample_app_4th_ed)
and extend it by building an API for the app.
## Designing our API
Designing an API is not an easy process.
Usually it's very difficult to know beforehand what the client will need.
However we will make our best to support most clients needs:
* have a resty approach using the popular JSONAPI spec
* use hypermedia for related resources instead of embedding them
* have in the same response data that otherwise would require many requests in the client
By the way, there is a long discussion about what REST means. Is just JSONAPI as REST as Joy Fielding's defined it?
Definitely not. However, it's more resty than regular JSON response, plus it has a wide support in terms of libraries.
Moving forward, let's add our first resource, let it be a user. But before adding the controller let's add the routes first:
``` ruby
#api
namespace :api do
namespace :v1 do
resources :sessions, only: [:create, :show]
resources :users, only: [:index, :create, :show, :update, :destroy] do
post :activate, on: :collection
resources :followers, only: [:index, :destroy]
resources :followings, only: [:index, :destroy] do
post :create, on: :member
end
resource :feed, only: [:show]
end
resources :microposts, only: [:index, :create, :show, :update, :destroy]
end
end
```
All REST routes for each record, only GET method for collections (Rails muddles up collection REST routes with
element REST routes in the same controllers) and a couple custom routes.
As you can see we have many routes. The idea is that the tutorial will mostly touch
and show you a couple of them and you will manage to understand and see the rest from
the code inside the repo. I think extended tutorials are boring :).
However, if you find something weird or you don't understand something you are always welcomed to
open an issue and ask :)
Let's create the users API controller and add support for the GET method on a single record:
## Adding our first API resource
The first thing we need to do is to separate our API from the rest of the app.
In order to do that we will create a new Controller under a different namespace.
Given that it's good to have versioned API let's go and create our first controller
under `app/controllers/api/v1/`
``` ruby
class Api::V1::BaseController < ActionController::API
end
```
As you can see we inherit from `ActionController::API` instead of `ActionController::Base`.
The former cuts down some features not needed making it a bit faster and less memory hungry :)
Now let's add the `users#show` action:
``` ruby
class Api::V1::UsersController < Api::V1::BaseController
def show
user = User.find(params[:id])
render jsonapi: user, serializer: Api::V1::UserSerializer
end
end
```
One thing that I like building APIs in Rails is that controllers are super clean _by default_.
We just request the user from the database and render it in JSON using AMS.
Let's add the user serializer under `app/serializers/api/v1/user_serializer.rb`.
We will use [ActiveModelSerializers](https://github.com/rails-api/active_model_serializers) for the JSON serialization.
``` ruby
class Api::V1::UserSerializer < ActiveModel::SExcerpt of 40,798 characters
Read on GitHubWould you bet a product on this? Bounded 0–100 and slow moving.
matched fp:2b8c4a60f0e88a81, topic:tutorial, name:tutorial, desc:tutorial