Use cache_digests with XML builder?

Can I use the new caching strategy in Rails 4 (cache_digests) for XML?

I suppose I can use xml.erb views, but I prefer xml.builder views for their patience.

Is there a way to use cache_digests this way?

+5
source share
1 answer

To use fragment caching and Rails 4 cache_digests in XML Builder files, simply use a method cachethat works exactly the same as in other templates. Here is an example of caching a Russian doll (simplified) RSS feed blog:

# feed.xml.builder

xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0", 'xmlns:atom': 'http://www.w3.org/2005/Atom' do
  xml.channel do
    xml.title "My Blog"

    cache "articles/feed-#{@articles.count}-#{@articles.maximum(:updated_at).try(:to_i)}" do
      @articles.each do |article|
        cache article do
          xml.item do
            xml.title article.title
            xml.description article.body
          end
        end
      end
    end
  end
end
+6
source

All Articles