Table of Content
Introducing jbuilder
In the past, we output JSON in controller.
1format.json { render json: @jobs }
But it has been like a hacking with lot’s of nesting block while the JSON format grows.
But the controller JSON story is over. Now we can define how the data is formatted in JSON, as same as how the data is organized in HTML. This are done by a gem named “JBuilder” from the official rails team. And it is now pre-installed in rails version 4.
In the Gemfile
, uncomment the following line and bundle install
it.
gem 'jbuilder'
JBuilder gem uses file extension .json.jbuilder
. This follows the extension convention like .html.erb
.
What’s next? We’re going to take a look at “Using jbuilder”.