instance method cache

Ruby on Rails 3.0.20

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

cache(name = {}, options = nil, &block)

This helper to exposes a method for caching of view fragments. See ActionController::Caching::Fragments for usage instructions.

A method for caching fragments of a view rather than an entire action or page. This technique is useful caching pieces like menus, lists of news topics, static HTML fragments, and so on. This method takes a block that contains the content you wish to cache. See ActionController::Caching::Fragments for more information.

Examples

If you wanted to cache a navigation menu, you could do the following.

<% cache do %>
  <%= render :partial => "menu" %>
<% end %>

You can also cache static content…

<% cache do %>
   <p>Hello users!  Welcome to our website!</p>
<% end %>

…and static content mixed with RHTML content.

<% cache do %>
  Topics:
  <%= render :partial => "topics", :collection => @topic_list %>
  <i>Topics listed alphabetically</i>
<% end %>

Parameters

name opt = {}
options opt = nil
block block
Source
# File actionpack/lib/action_view/helpers/cache_helper.rb, line 36
      def cache(name = {}, options = nil, &block)
        if controller.perform_caching
          safe_concat(fragment_for(name, options, &block))
        else
          yield
        end

        nil
      end

Defined in actionpack/lib/action_view/helpers/cache_helper.rb line 36 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::CacheHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close