top of page

Why I Still Use Jbuilder in Rails APIs

  • Foto do escritor: RToledoDev
    RToledoDev
  • 19 de jul.
  • 1 min de leitura

Atualizado: 23 de jul.

Jbuilder example

While it's trendy to reach for serializers, blueprints, or JSON:API gems, I still reach for Jbuilder in most of my Rails API projects — and here's why.

🚀 It's Simple and Explicit

I like tools that don't hide magic. With Jbuilder, I know exactly what I'm returning.




✅ A basic example:

def index
  @books = Book.all
  render :index, formats: :json
end
json.books @books do |book|
  json.extract! book, :id, :title, :author
end

No macros. No DSL. Just clean JSON you control using Jbuilder

🧠 A more real-world example (with model logic)

I often expose computed fields directly from models.

📘 Let's say we have this model:

# app/models/book.rb

def available_copies
  total_copies - borrowings.where(returned_at: nil).count
end

Now expose that logic in the response:

# app/views/api/v1/books/index.json.jbuilder

json.books @books do |book|
  json.extract! book, :id, :title, :author, :total_copies
  json.available_copies book.available_copies
  json.borrowings book.borrowings do |borrowing|
    json.extract! borrowing, :id, :due_at, :returned_at
    json.user do
      json.email_address borrowing.user.email_address
    end
  end
end

Clean, readable, and reflects actual application logic — not just raw database fields.

🙅🏻‍♂️ I Don’t Need Fancy

If my API grows, I can refactor later to jsonapi-serializer, blueprinter, or even GraphQL. But for most APIs?

Jbuilder works. It’s built-in. It’s enough.

✅ TL;DR

  • ✅ Explicit structure

  • ✅ Built into Rails

  • ✅ Easy to customize and extend

  • ✅ No hidden behavior

Use what works. Don't overthink it. Sometimes the simple way is the right way.

Comentários


rails-rtoledo_edited.png

Hi, I'm Rodrigo Toledo

A full-stack developer skilled in both front-end and back-end development, building and maintaining web applications

  • Facebook
  • Youtube
  • LinkedIn
  • Instagram

I don't know everything, help me

I'm always seeking to improve what I build and to refine my skills. Whenever I master something, I share it. And when I don't understand, I dive deep until I do—learning, growing, and helping others along the way.

Subscribe

Thanks for submitting!

bottom of page