Ready to Work Together?
Let's discuss how our expertise can help transform your business.
"Zee" Zivkovic
·
Apr 18, 2023
Rabl is a powerful tool for building a Rails JSON API. It allows you to easily return JSON responses from your APIs in a clear and concise way. And separate the logic of your API from the presentation of the data.
With that said, the Rabl syntax can be hard to configure initially:
A missing comma or symbol in one file can lead the whole serializer to crash, leaving you with confusing error messages.
This article will help you get set up with Rabl and will cover the following three points:
If you’d like to read more about how to send requests to your Rabl API, or if you’re looking for more advanced Rabl techniques, read our follow-up articles that dive deeper into each of these topics.
There are several reasons why you might use Rabl over a traditional Rails view:
Now, let’s get into the Rails API tutorial.
The initial setup for Rabl is similar to most Ruby gems:
Add the library (along with the related gem oj) to your Gemfile and run bundle install.
Note: Full Rabl setup instructions can be found here.
# Gemfile
gem 'rabl'
gem 'oj
bundle install
Our structure typically involves a call from our front end to one of the API endpoints.
So, if you’re comfortable with Rails routes and controllers, there’s good news: You still get to write the route, the model, and the controller in exactly the way that you’re used to.
First, create a model for your users:
rails g model User first_name:string last_name:string
rails db:migrate
Add routes for the user:
config/routes.rb
resources :users, only: [:show, :index]
Create a users_controller.rb file in app/controllers:
app/controllers/users_controller.rb
def show
@users = User.all
end
def show
@user = User.find(params[:id])
end
You can’t fetch any JSON data from your API if your database is empty. In your Rails console, create at least one user.
User.create(first_name: "Test", last_name: "User")
With that foundation set up, we’ll begin configuring our Rabl files.
The Rabl file lives in the same place where you would normally locate a Ruby view file: In the views directory of your app folder. After all, that’s where Rails will look to next after going through the controller.
# app/views/users/show.rabl
child @user => :result do
extends 'users/_user'
end
Two things to notice here:
First, the file is named with the controller action that will call it, “show.”
Second, this file uses a .rabl extension, rather than .html.erb. This signals to the Rabl gem to wake up and do its work.
There are two lines to include in this file:
The “object” line tells Rabl to use the @user variable as the source of data for the template. (This is the instance variable that we created in the controller.)
The “attributes” line specifies which user attributes should be included in the JSON representation of the user. In this example, we’ve included the first name and last name. If you wished, you could also include the :id. Similar to the show file, here is an example of an index endpoint that will list data for all users:
# app/views/users/index.rabl
node(:total_records) do
User.all.count
end
child @users => :result do
extends 'users/_user'
end
And the final piece is to add the partial with the actual attributes for the user record itself. With our user model this can be as simple as the following:
# app/views/users/_user.rabl
attributes :id, :first_name, :last_name
Now you’ve got your Rails API serializer files. The next step is…
Nothing.
That’s it. You now have a functional API endpoint!
But don’t take our word for it. You can test the endpoint using a tool like Postman or Curl to send a GET request. Or you can visit the endpoint in your browser. If you haven’t yet started your Rails server, you’ll need to do that.
rails s
And then, assuming that your Rails app is running on Port 3000, you’ll visit this url in your browser window:
You should see your user object’s JSON response:
To fully control how your API endpoints send data, you will need to customize the Rabl initializer.
Many of the configurations here come down to your team’s preferences But we recommend keeping your Rabl initializer relatively simple with just the following lines:
# config/intializers/rabl_init.rb
require "rabl"
Rabl.configure do |config|
config.include_json_root = false
config.include_child_root = false
config.view_paths = ["app/views/"]
end
Step 3: Conclusion
Now that you can receive useful JSON responses to your API requests, you’re free to organize that data in your front-end framework of choice.
In the next article, we’ll detail how NextLink Labs uses axios in our React front-ends to fetch this data.
If your team is struggling with technical debt as you build your custom software applications, we’re happy to provide additional guidance!
Author at NextLink Labs
Custom Software Development
Large Rails monoliths burn millions of tokens per AI session. These 5 architectural changes cut costs and boost AI suggestion quality by 3-4x.
Colin Soleim
·
Feb 19, 2026
Custom Software Development
How to Setup Ruby's YJIT Compiler in Your Rails Application
Colin Soleim
·
Mar 28, 2024
Custom Software Development
Inclusive web design means making web experiences accessible and user-friendly for everyone. Learn the principles of web application accessibility today!
Jared Blumer
·
Oct 4, 2023
Custom Software Development
Ruby is an open-source, object-oriented language that focuses on flexibility and readability. Discover the pros and cons of the Ruby programming language!
Dustin Gault
·
Sep 29, 2023
Let's discuss how our expertise can help transform your business.