@@ -5,7 +5,7 @@ manipulating giant hash structures. This is particularly helpful when the
5
5
generation process is fraught with conditionals and loops. Here's a simple
6
6
example:
7
7
8
- ``` ruby
8
+ ``` ruby
9
9
# app/views/messages/show.json.jbuilder
10
10
11
11
json.content format_content(@message .content)
31
31
32
32
This will build the following structure:
33
33
34
- ``` javascript
34
+ ``` javascript
35
35
{
36
36
" content" : " <p>This is <i>serious</i> monkey business</p>" ,
37
37
" created_at" : " 2011-10-29T20:45:28-05:00" ,
@@ -57,20 +57,23 @@ This will build the following structure:
57
57
}
58
58
```
59
59
60
+ ## Dynamically Defined Attributes
61
+
60
62
To define attribute and structure names dynamically, use the ` set! ` method:
61
63
62
- ``` ruby
64
+ ``` ruby
63
65
json.set! :author do
64
66
json.set! :name , ' David'
65
67
end
66
68
67
69
# => {"author": { "name": "David" }}
68
70
```
69
71
72
+ ## Merging Existing Hash or Array
70
73
71
74
To merge existing hash or array to current context:
72
75
73
- ``` ruby
76
+ ``` ruby
74
77
hash = { author: { name: " David" } }
75
78
json.post do
76
79
json.title " Merge HOWTO"
80
83
# => "post": { "title": "Merge HOWTO", "author": { "name": "David" } }
81
84
```
82
85
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.
84
89
85
- ``` ruby
90
+ ``` ruby
86
91
# @comments = @post.comments
87
92
88
93
json.array! @comments do |comment |
98
103
# => [ { "body": "great post...", "author": { "first_name": "Joe", "last_name": "Bloe" }} ]
99
104
```
100
105
106
+ ## Array Attributes
107
+
101
108
You can also extract attributes from array directly.
102
109
103
- ``` ruby
110
+ ``` ruby
104
111
# @people = People.all
105
112
106
113
json.array! @people , :id , :name
107
114
108
115
# => [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ]
109
116
```
110
117
118
+ ## Plain Arrays
119
+
111
120
To make a plain array without keys, construct and pass in a standard Ruby array.
112
121
113
122
``` ruby
@@ -118,6 +127,8 @@ json.people my_array
118
127
# => "people": [ "David", "Jamie" ]
119
128
```
120
129
130
+ ## Child Objects
131
+
121
132
You don't always have or need a collection when building an array.
122
133
123
134
``` ruby
135
146
# => { "people": [ { "id": 1, "name": "David" }, { "id": 2, "name": "Jamie" } ] }
136
147
```
137
148
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.
139
152
140
- ``` ruby
153
+ ``` ruby
141
154
class Person
142
155
# ... Class Definition ... #
143
156
def to_builder
@@ -163,11 +176,13 @@ company.to_builder.target!
163
176
# => {"name":"Doodle Corp","president":{"name":"John Stobs","age":58}}
164
177
```
165
178
179
+ ## Rails Integration
180
+
166
181
You can either use Jbuilder stand-alone or directly as an ActionView template
167
182
language. When required in Rails, you can create views à la show.json.jbuilder
168
183
(the json is already yielded):
169
184
170
- ``` ruby
185
+ ``` ruby
171
186
# Any helpers available to views are available to the builder
172
187
json.content format_content(@message .content)
173
188
json.(@message , :created_at , :updated_at )
@@ -183,6 +198,8 @@ if current_user.admin?
183
198
end
184
199
```
185
200
201
+ ## Partials
202
+
186
203
You can use partials as well. The following will render the file
187
204
` views/comments/_comments.json.jbuilder ` , and set a local variable
188
205
` 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`.
215
232
Be sure not to confuse the ` as: ` option to mean nesting of the partial. For example:
216
233
217
234
``` 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
221
238
```
222
239
223
240
is quite different from:
224
241
225
242
``` ruby
226
- # comment attributes are nested under a "comment" property
243
+ # comment attributes are nested under a "comment" property
227
244
json.comment do
228
245
json.partial! " /comments/comment.json.jbuilder" , comment: @comment
229
246
end
@@ -239,10 +256,11 @@ json.partial! 'sub_template', locals: { user: user }
239
256
json.partial! ' sub_template' , user: user
240
257
```
241
258
259
+ ## Null Values
242
260
243
261
You can explicitly make Jbuilder object return null if you want:
244
262
245
- ``` ruby
263
+ ``` ruby
246
264
json.extract! @post , :id , :title , :content , :published_at
247
265
json.author do
248
266
if @post .anonymous?
@@ -305,7 +323,7 @@ This will include both records as part of the cache key and updating either of t
305
323
Keys can be auto formatted using ` key_format! ` , this can be used to convert
306
324
keynames from the standard ruby_format to camelCase:
307
325
308
- ``` ruby
326
+ ``` ruby
309
327
json.key_format! camelize: :lower
310
328
json.first_name ' David'
311
329
@@ -315,15 +333,15 @@ json.first_name 'David'
315
333
You can set this globally with the class method ` key_format ` (from inside your
316
334
environment.rb for example):
317
335
318
- ``` ruby
336
+ ``` ruby
319
337
Jbuilder .key_format camelize: :lower
320
338
```
321
339
322
340
By default, key format is not applied to keys of hashes that are
323
341
passed to methods like ` set! ` , ` array! ` or ` merge! ` . You can opt into
324
342
deeply transforming these as well:
325
343
326
- ``` ruby
344
+ ``` ruby
327
345
json.key_format! camelize: :lower
328
346
json.deep_format_keys!
329
347
json.settings([{some_value: " abc" }])
@@ -334,7 +352,7 @@ json.settings([{some_value: "abc"}])
334
352
You can set this globally with the class method ` deep_format_keys ` (from inside your
335
353
environment.rb for example):
336
354
337
- ``` ruby
355
+ ``` ruby
338
356
Jbuilder .deep_format_keys true
339
357
```
340
358
@@ -350,4 +368,5 @@ features and discuss issues.
350
368
See [ CONTRIBUTING] ( CONTRIBUTING.md ) .
351
369
352
370
## License
371
+
353
372
Jbuilder is released under the [ MIT License] ( http://www.opensource.org/licenses/MIT ) .
0 commit comments