Method: Jbuilder#child!

Defined in:
lib/jbuilder.rb

#child!Object

Turns the current element into an array and yields a builder to add a hash.

Example:

json.comments do
  json.child! { json.content "hello" }
  json.child! { json.content "world" }
end

{ "comments": [ { "content": "hello" }, { "content": "world" } ]}

More commonly, you’d use the combined iterator, though:

json.comments(@post.comments) do |comment|
  json.content comment.formatted_content
end


178
179
180
181
# File 'lib/jbuilder.rb', line 178

def child!
  @attributes = [] unless ::Array === @attributes
  @attributes << _scope{ yield self }
end