Rails building json API responses with JBuilder
Rails is a MVC web application framework which supports building complex websites without writing any APIs. But, client side libraries are evolving at brisk pace with wide-spread use of React JS, AngularJS, VuJS etc. Thus, building API endpoints is a very important tasks these days.
Rails has been pretty adoptive when it comes to supporting what community is up to. Rails started supporting API only application from Rails 5. There are quite a few options to build json response in Rails application. Listing down a couple of widely used gems to build them.
Both of them have their pros and cons to consider. In this article, we will just consider building json API response with jbuilder.
JBuilder
Jbuilder is quite useful when it comes to generating and rendering JSON responses for API requests in Rails. This article helps understand, how to render JSON responses in general. Jbuilder is particularly good at following things:
- Reusable JSON API responses through partials
- View helpers available when generating API responses
- Convention over configuration principle in action
We will also take a look at rendering json reopnses from controller with JBuilder. Then, reusing same JSON reponse in jbuilder view file and models.
Rails API only application
Rails API only application is a slimmed down version of Rails application. It does not contain gems that are usually required for front-end specific implementation. For e.g. coffee rails, turbolinks etc.
Ruby / Rails Versions
Versions used in building this application are listed below.
- Ruby: 2.5.3
- Rails: 5.2.1
1. Create Rails API only application
Let us create an API only Rails application with the help of command given below.
-
--api
option takes care of setting up Rails application to beapi
only application. -
This command also performs bundle install to resolve dependencies and install them in order to run the application.
2. Add jbuilder gem
Rails API only app that is generated in Step 1, has following lines in commented format as given below.
In order to use jbuilder to build/format json, we need to uncomment those two lines or add
to Gemfile.
Bundle install to install the gem.
3. Create Data Model
In order to demonstrat how to build APIs with API only Rails application, let us create a few tables and insert data into them.
Once, we run generators to create migrations and models, let’s run these migrations.
4. Insert data
We will use faker to quickly add some dummy data to the data model that we created in Step 3.
Add gem faker to Gemfile.
We will just use faker gem to generate some fake data in development and test environments. Then, perform
to install faker gem and to use it with our application.
We will add fake data using seeds.rb
Run database seed data generation with the command given below.
Render JSON responses with JBuilder
Now that we have Rails API only application ready with jbuilder and some data in place, we can use jbuilder to repond with json to API requests.
Render array in API response with JBuilder
Let’s say we want a list of categories in an API request.
Step 1. Create categories controller.
Step 2. Setup route for API endpoint
This step will setup RESTful routes for categories resource.
Step 3. Create controller action
Step 4. Create index.json.jbuilder file in category views
Create a directory categories
if it does not exist.
Run the command given below from project root.
Create a file index.json.jbuilder in app/views/categories directory.
Step 5. JSON response from jbuilder
To verify the response.
- Start the rails server.
- Request to categories API endpoints
JSON response will be something like,
Obviously, the above listed json is a trimmed down version of the original response.
Rendering Partials in API response with JBuilder
We can use partials to render json response which is required in multiple APIs.
Let’s say, we want a list of articles in an API endpoints.
Step 1 Create ArticlesController
Create ArticlesController with the help of Rails generator with command given below.
Step 2: Setup Routes for articles resource
Setup routes for *Article resource with the help of command given below.
Step 3. Create controller action
Step 4. Create index.json.jbuilder file in articles views
Create a directory articles if it does not exist . Run the command given below from project root.
Create a file index.json.jbuilder in app/views/articles directory.
Step 5. JSON response from jbuilder
To verify the response.
- Start the rails server.
- Request to categories API endpoints
JSON response will be something like,
Note: The response given above is limited to 2 articles.
Next, let us add a category for each article in json with partial in Jbuilder
Step 6 Create partials jbuilder for category
Create a partial view file in app/views/categories directory, by name _category.json.jbuilder
Step 7 Render partial in Article json jbuilder file
We can call partial in jbuilder to generate json as given below.
This will render category for each article, when articles.json is called.
curl -X http://localhost:3000/articles.json
Response:
Note: Response is limited to 2 results.
As we can see, each article node in the json response contains category node with details of the category. This gets rendered by the jbuilder partial which was generated for category.
Feel free to comment down with your thoughts. Thanks for reading!
References:
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox