Use partials in Jbuilder
mainJbuilder supports rendering partials. You can render a partial with specific locals, render an object to a partial under a key, or render collections of partials.
Key options:
locals: A hash of variables to pass to the partial.as: Maps the object (or collection item) to a specific variable name within the partial.collection: Used to render a collection of partials.
Note: as: defines the variable name in the partial, it does not define the nesting level of the JSON output.
# Render a partial with locals
json.partial! 'sub_template', locals: { user: user }
# or
json.partial! 'sub_template', user: user
# Render an object to a partial under a key
json.post @post, partial: 'posts/post', as: :post
# Render collections of partials
json.array! @posts, partial: 'posts/post', as: :post
json.partial! 'posts/post', collection: @posts, as: :post
# Render collections of partials under a key
json.comments @post.comments, partial: 'comments/comment', as: :comment