diff options
Diffstat (limited to 'guides/source/caching_with_rails.textile')
-rw-r--r-- | guides/source/caching_with_rails.textile | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/guides/source/caching_with_rails.textile b/guides/source/caching_with_rails.textile index f6d6c8b550..9f1ac18814 100644 --- a/guides/source/caching_with_rails.textile +++ b/guides/source/caching_with_rails.textile @@ -439,7 +439,7 @@ class ProductsController < ApplicationController # If the request is stale according to the given timestamp and etag value # (i.e. it needs to be processed again) then execute this block - if stale?(:last_modified => @product.updated_at.utc, :etag => @product) + if stale?(:last_modified => @product.updated_at.utc, :etag => @product.cache_key) respond_to do |wants| # ... normal response processing end @@ -453,6 +453,17 @@ class ProductsController < ApplicationController end </ruby> +Instead of a options hash, you can also simply pass in a model, Rails will use the methods +updated_at+ and +cache_key+ for setting +last_modified+ and +etag+: + +<ruby> +class ProductsController < ApplicationController + def show + @product = Product.find(params[:id]) + respond_with(@product) if stale?(@product) + end +end +</ruby> + If you don't have any special response processing and are using the default rendering mechanism (i.e. you're not using respond_to or calling render yourself) then you’ve got an easy helper in fresh_when: <ruby> |