Codementor Events

Creating a Simple API with Rails

Published Jan 05, 2016Last updated Apr 12, 2017
Creating a Simple API with Rails

Creating Simple APIS with Rails

You probably have heard talk about Rails and how Rails is handy when creating web-apps. In this tutorial, I will try to explain in a few words how to create simple and RESTful APIs with Rails.

Rails-API

Rails::API is a subset of a normal Rails application, created for applications that don't require all functionality as a web app. For example, assets, and frontend things such as CSS and JavaScript.

Using Rails-API will allow you to utilize the following benefits from Rails.

  • URL generation
  • Resourceful generation
  • Header and Redirection Responses
  • Plugins: Many third-party libraries come with support for Rails

Related to many third-party libraries, we are using in this tutorial Active Model Serializers.

Active Model Serializers

Active Model Serializers brings convention over configuration to JSON generation. Basically, it has adapters and serializers.

  • Adapters: Describe which attributes and relationships should be serialized.
  • Serializers: Describe how attributes and relationships should be serialized.

Active Model Serializer is under-development version in Rails 5, and Rails 5 will use AMS by default.

Creating our API

Our API will be for a TODO List application.

For this tutorial I'm using:

  • Rails 4.2.1 (remembering rails-api will be included on rails 5)
  • Ruby 2.1.1
rails-api new todo

Doing this will create the application without assets/helpers/views. As you can see here

Add Active Model Serializer in your Gemfile

gem 'active_model_serializers', '~> 0.10.0.rc2'

  • Creating a model

In our API, we will have a model called Task.

bash rails g scaffold task title completed:boolean order:integer

  • Creating a serializer

bash rails g serializer task title completed order

The serializer should look like:

class TaskSerializer < ActiveModel::Serializer
  attributes :id, :title, :completed
end

Here is one interesting thing about AMS and Serializers: If you have some relationship between models, you can just use a similar Active Record syntax in your serializer: has_many or belongs_to.

Let's try to test our API and send a request:

curl -H "Content-Type:application/json; charset=utf-8" -d "'task':{'title':'something to do','order':1,'completed':false}" http://localhost:3000/tasks

And we can check in the browser:

Our data serialized

As you can see above, it worked out! There are others interesting aspects about Active Model Serializers and you can check it here.

Hope you have enjoyed the combo Rails-API + AMS!

Discover and read more posts from Francisco José L. Magalhães (Franzé)
get started
post commentsBe the first to share your opinion
Abhijith R
6 years ago

USE this instead if you got an error page:

curl -H “Content-Type:application/json; charset=utf-8” -d ‘{"task
":{“title”:“something to do”, “completed”:false, “order”:1}}’ http://localhost:3000/tasks

Oliver Broad
8 years ago

I had to change the curl request for it to work properly and avoid ActionDispatch::ParamsParser::ParseError. May be useful for someone else trying out this tutorial and running into the same problem ->
curl -H “Content-Type:application/json; charset=utf-8” -d ‘{“title”:“something to do”, “completed”:false}’ http://localhost:3000/tasks

John Kevin Basco
8 years ago

Sweet post! Thanks. This is really nice for simple apis but when you need to develop a bigger and complex api, its nice to consider using rails + jsonapi resources w/c uses jsonapi spec. It lets you focus more on your actual application. I started writing a tutorial series about it - http://tutorialsfordevs.com…

Show more replies