@@ -5,7 +5,7 @@ manipulating giant hash structures. This is particularly helpful when the
55generation process is fraught with conditionals and loops. Here's a simple
66example:
77
8- ``` ruby
8+ ``` ruby
99# app/views/messages/show.json.jbuilder
1010
1111json.content format_content(@message .content)
3131
3232This will build the following structure:
3333
34- ``` javascript
34+ ``` javascript
3535{
3636 " content" : " <p>This is <i>serious</i> monkey business</p>" ,
3737 " created_at" : " 2011-10-29T20:45:28-05:00" ,
@@ -57,20 +57,23 @@ This will build the following structure:
5757}
5858```
5959
60+ ## Dynamically Defined Attributes
61+
6062To define attribute and structure names dynamically, use the ` set! ` method:
6163
62- ``` ruby
64+ ``` ruby
6365json.set! :author do
6466 json.set! :name , ' David'
6567end
6668
6769# => {"author": { "name": "David" }}
6870```
6971
72+ ## Merging Existing Hash or Array
7073
7174To merge existing hash or array to current context:
7275
73- ``` ruby
76+ ``` ruby
7477hash = { author: { name: " David" } }
7578json.post do
7679 json.title " Merge HOWTO"
8083# => "post": { "title": "Merge HOWTO", "author": { "name": "David" } }
8184```
8285
83- Top level arrays can be handled directly. Useful for index and other collection actions.
86+ ## Top Level Arrays
87+
88+ Top level arrays can be handled directly. Useful for index and other collection actions.
8489
85- ``` ruby
90+ ``` ruby
8691# @comments = @post.comments
8792
8893json.array! @comments do |comment |
98103# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
99104```
100105
106+ ## Array Attributes
107+
101108You can also extract attributes from array directly.
102109
103- ``` ruby
110+ ``` ruby
104111# @people = People.all
105112
106113json.array! @people , :id , :name
107114
108115# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
109116```
110117
118+ ## Plain Arrays
119+
111120To make a plain array without keys, construct and pass in a standard Ruby array.
112121
113122``` ruby
@@ -118,6 +127,8 @@ json.people my_array
118127# => "people": [ "David", "Jamie" ]
119128```
120129
130+ ## Child Objects
131+
121132You don't always have or need a collection when building an array.
122133
123134``` ruby
135146# => { "people": [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] }
136147```
137148
138- Jbuilder objects can be directly nested inside each other. Useful for composing objects.
149+ ## Nested Jbuilder Objects
150+
151+ Jbuilder objects can be directly nested inside each other. Useful for composing objects.
139152
140- ``` ruby
153+ ``` ruby
141154class Person
142155 # ... Class Definition ... #
143156 def to_builder
@@ -163,11 +176,13 @@ company.to_builder.target!
163176# => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
164177```
165178
179+ ## Rails Integration
180+
166181You can either use Jbuilder stand-alone or directly as an ActionView template
167182language. When required in Rails, you can create views à la show.json.jbuilder
168183(the json is already yielded):
169184
170- ``` ruby
185+ ``` ruby
171186# Any helpers available to views are available to the builder
172187json.content format_content(@message .content)
173188json.(@message , :created_at , :updated_at )
@@ -183,6 +198,8 @@ if current_user.admin?
183198end
184199```
185200
201+ ## Partials
202+
186203You can use partials as well. The following will render the file
187204` views/comments/_comments.json.jbuilder ` , and set a local variable
188205` comments ` with all this message's comments, which you can use inside
@@ -215,15 +232,15 @@ then the object is passed to the partial as the variable `some_symbol`.
215232Be sure not to confuse the ` as: ` option to mean nesting of the partial. For example:
216233
217234``` ruby
218- # Use the default `views/comments/_comment.json.jbuilder`, putting @comment as the comment local variable.
219- # Note, `comment` attributes are "inlined".
220- json.partial! @comment , as: :comment
235+ # Use the default `views/comments/_comment.json.jbuilder`, putting @comment as the comment local variable.
236+ # Note, `comment` attributes are "inlined".
237+ json.partial! @comment , as: :comment
221238```
222239
223240is quite different from:
224241
225242``` ruby
226- # comment attributes are nested under a "comment" property
243+ # comment attributes are nested under a "comment" property
227244json.comment do
228245 json.partial! " /comments/comment.json.jbuilder" , comment: @comment
229246end
@@ -239,10 +256,11 @@ json.partial! 'sub_template', locals: { user: user }
239256json.partial! ' sub_template' , user: user
240257```
241258
259+ ## Null Values
242260
243261You can explicitly make Jbuilder object return null if you want:
244262
245- ``` ruby
263+ ``` ruby
246264json.extract! @post , :id , :title , :content , :published_at
247265json.author do
248266 if @post .anonymous?
@@ -305,7 +323,7 @@ This will include both records as part of the cache key and updating either of t
305323Keys can be auto formatted using ` key_format! ` , this can be used to convert
306324keynames from the standard ruby_format to camelCase:
307325
308- ``` ruby
326+ ``` ruby
309327json.key_format! camelize: :lower
310328json.first_name ' David'
311329
@@ -315,15 +333,15 @@ json.first_name 'David'
315333You can set this globally with the class method ` key_format ` (from inside your
316334environment.rb for example):
317335
318- ``` ruby
336+ ``` ruby
319337Jbuilder .key_format camelize: :lower
320338```
321339
322340By default, key format is not applied to keys of hashes that are
323341passed to methods like ` set! ` , ` array! ` or ` merge! ` . You can opt into
324342deeply transforming these as well:
325343
326- ``` ruby
344+ ``` ruby
327345json.key_format! camelize: :lower
328346json.deep_format_keys!
329347json.settings([{some_value: " abc" }])
@@ -334,7 +352,7 @@ json.settings([{some_value: "abc"}])
334352You can set this globally with the class method ` deep_format_keys ` (from inside your
335353environment.rb for example):
336354
337- ``` ruby
355+ ``` ruby
338356Jbuilder .deep_format_keys true
339357```
340358
@@ -350,4 +368,5 @@ features and discuss issues.
350368See [ CONTRIBUTING] ( CONTRIBUTING.md ) .
351369
352370## License
371+
353372Jbuilder is released under the [ MIT License] ( http://www.opensource.org/licenses/MIT ) .
0 commit comments