Se você ainda não sabe o que são ETags, leia o último post desta série.
Este recurso já foi efetivamente incluido no branch oficial do Rails, mas junto com ele também ganhamos mais dois novos métodos que devem facilitar a forma como vamos implementar isto em nosso código. Os métodos etag! e last_modified!.
Veja a implementação destes métodos direto do código fonte do Rails:
# Sets the Last-Modified response header. Returns 304 Not Modified if the
# If-Modified-Since request header is <= last modified.
def last_modified!(utc_time)
head(:not_modified) if response.last_modified!(utc_time)
end
# Sets the ETag response header. Returns 304 Not Modified if the
# If-None-Match request header matches.
def etag!(etag)
head(:not_modified) if response.etag!(etag)
end
Isto facilita muito as coisas. Veja como ficaria o exemplo dado no último post usando estes novos métodos:
class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
etag! @article
last_modified! @article.published_at.utc
end
end
Bem mais simples!