Junior Track Roadmap

API development: Jbuilder

Jbuilder builds JSON responses in Rails through ordinary method calls on an object named json. The lessons read the .json.jbuilder views of one app line by line, along with the installed jbuilder 2.15.1 source. They cover the implicit setter that routes calls through method_missing to set!, json.array! for collections, and json.partial! for shared shapes. They also explain how Rails registers the jbuilder handler and picks a template for a JSON request. Last, they compare shaped views with calling to_json or as_json on a model.

What you'll learn

  • ✓How implicit calls like json.status reach set! through method_missing
  • ✓Building nested objects and arrays under a key with set! and a block
  • ✓Turning a mixed collection into a JSON array with json.array!
  • ✓What json.partial! does and when a shared JSON shape calls for it
  • ✓How Rails registers the jbuilder handler and finds a .json.jbuilder file for a request
  • ✓Why shaping JSON in a view differs from calling to_json or as_json on a model
Free preview · Lesson 1 2 min read

Six .json.jbuilder views, already shipping in this app

On a development database, that returns:

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...