Implementing Jbuilder for creating JSON Response in Ruby on Rails

This article will discuss how to implement JBuilder for creating JSON response in Ruby on Rails.

Create a new application

1
2
rails new jbuilder-tutorial --api
cd jbuilder-tutorial

Scaffolding

1
2
rails g scaffold Post title body:text
rails db:create db:migrate

Installing Dependencies

1
gem 'jbuilder'
1
bundle install

Seeding the data

In your db/seeds.rb put in some data like this:

1
2
3
5.times do |i|
 Post.create(title: "Post Number #{i}", body: "This is the body of post number #{i}")
end

Implementation

Remove all the lines that say render json and make your app/controllers/posts_controller.rb look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# app/controllers/posts_controller.rb

class PostsController < ApplicationController
 before_action :set_post, only: %i[show update destroy]

 # GET /posts
 def index
   @posts = Post.all
 end

 # GET /posts/1
 def show; end

 # POST /posts
 def create
   @post = Post.new(post_params)

   if @post.save
     @post
   else
     render json: @post.errors, status: :unprocessable_entity
   end
 end

 # PATCH/PUT /posts/1
 def update
   if @post.update(post_params)
     @post
   else
     render json: @post.errors, status: :unprocessable_entity
   end
 end

 # DELETE /posts/1
 def destroy
   @post.destroy
 end

 private
 # Use callbacks to share common setup or constraints between actions.
 def set_post
   @post = Post.find(params[:id])
 end

 # Only allow a trusted parameter "white list" through.
 def post_params
   params.require(:post).permit(:title, :body)
 end
end

Now create jbuilder views.

1
2
3
4
5
6
mkdir app/views/posts
touch app/views/posts/index.json.jbuilder
touch app/views/posts/show.json.jbuilder
touch app/views/posts/update.json.jbuilder
touch app/views/posts/create.json.jbuilder
touch app/views/posts/destroy.json.jbuilder

Make touch app/views/posts/index.json.jbuilder look like this:

1
2
3
4
5
json.array! @posts do |post|
 json.id post.id
 json.title post.title
 json.body post.body
end

You will get the response from http://localhost:3000/posts GET request like this:

[ { “id”: 1, “title”: “Post Number 0”, “body”: “This is the body of post number 0” }, { “id”: 2, “title”: “Post Number 1”, “body”: “This is the body of post number 1” }, … ]

The touch app/views/posts/show.json.jbuilder like this:

1
2
3
4
5
json.post do
 json.id @post.id
 json.title @post.title
 json.body @post.body
end

You will get a response from http://localhost:3000/posts/1 GET request like this:

{ “post”: { “id”: 1, “title”: “Post Number 0”, “body”: “This is the body of post number 0” } }

The touch app/views/posts/create.json.jbuilder like this:

1
2
3
4
5
json.post do
 json.id @post.id
 json.title @post.title
 json.body @post.body
end

You will get a response from http://localhost:3000/posts POST request with title and body params like this:

{ “post”: { “id”: 6, “title”: “This post is created”, “body”: “This is post body” } }

The touch app/views/posts/update.json.jbuilder like this:

1
2
3
4
5
json.post do
 json.id @post.id
 json.title @post.title
 json.body @post.body
end

You will get a similar response to that of the POST method.

Finally, the touch app/views/posts/destroy.json.jbuilder like this:

1
2
3
json.post do
 json.id @post.id
end

You will get a response from http://localhost:3000/posts/1 DELETE request like this:

{ “post”: { “id”: 1 } }

Now you know how to implement Jbuilder for creating JSON response in Ruby on Rails! Happy implementing!