instance method partial!

Ruby on Rails 2.13.0

Since v2.4.1 Last seen in v2.14.1

Available in: v2.4.1 v2.5.0 v2.6.4 v2.7.0 v2.8.0 v2.9.1 v2.10.2 v2.11.5 v2.12.0 v2.13.0 v2.14.1

Signature

partial!(*args)

Generates JSON using the template specified with the :partial option. For example, the code below will render the file views/comments/_comments.json.jbuilder, and set a local variable comments with all this message’s comments, which can be used inside the partial.

Example:

json.partial! 'comments/comments', comments: @message.comments

There are multiple ways to generate a collection of elements as JSON, as ilustrated below:

Example:

json.array! @posts, partial: 'posts/post', as: :post

# or:
json.partial! 'posts/post', collection: @posts, as: :post

# or:
json.partial! partial: 'posts/post', collection: @posts, as: :post

# or:
json.comments @post.comments, partial: 'comments/comment', as: :comment

Aside from that, the :cached options is available on Rails >= 6.0. This will cache the rendered results effectively using the multi fetch feature.

Example:

json.array! @posts, partial: "posts/post", as: :post, cached: true

json.comments @post.comments, partial: "comments/comment", as: :comment, cached: true

Parameters

args rest
Source
# File lib/jbuilder/jbuilder_template.rb, line 51
  def partial!(*args)
    if args.one? && _is_active_model?(args.first)
      _render_active_model_partial args.first
    else
      _render_explicit_partial(*args)
    end
  end

Defined in lib/jbuilder/jbuilder_template.rb line 51 · View on GitHub · Improve this page · Find usages on GitHub

Defined in JbuilderTemplate

Type at least 2 characters to search.

↑↓ navigate · open · esc close