# Fragment caching not expiring the correct fragment cache

**URL:** <https://www.ruby-forum.com/t/fragment-caching-not-expiring-the-correct-fragment-cache/153417>\
**Category:** Rails\
**Created:** [November 16, 2008, 11:09pm UTC](https://www.ruby-forum.com/t/fragment-caching-not-expiring-the-correct-fragment-cache/153417 "2008-11-16T23:09:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 16, 2008, 11:09pm UTC](https://www.ruby-forum.com/t/fragment-caching-not-expiring-the-correct-fragment-cache/153417/1 "2008-11-16T23:09:32Z")

</div>

Hi,

I have a Product model. My app creates a fragment cache for  
[mysite.com/products](http://mysite.com/products), which triggers the index action (my app is  
RESTful). Only admins can edit products, therefore, when adding a new  
product, the [mysite.com/products](http://mysite.com/products) should expire.

But I have a problem: my products administration controller is in  
admin/products\_controller.rb, and therefore stupid Rails expires  
admin/products fragment which is not correct.

Here is part of my code:

1. In the view:  
–  
\<%- cache(:controller =\> ‘products’, :action =\> ‘index’) do -%\>  
\<%- for product in @products -%\>  
…  
–

2. In the admin/products\_controller.rb:  
–  
cache\_sweeper :product\_sweeper, :only =\> [:create, :update, :destroy]  
–

3. And my sweeper:  
–  
class ProductSweeper \< ActionController::Caching::Sweeper  
observe Product

def after\_save(product)  
expire\_cache(product)  
end

def after\_destroy(product)  
expire\_cache(product)  
end

## def expire\_cache(product) expire\_fragment(:controller =\> ‘products’, :action =\> ‘index’) end end

How to tell Rails to not be clever, and simply force him to expire  
products fragment instead of admin/products?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [November 16, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/fragment-caching-not-expiring-the-correct-fragment-cache/153417/2 "2008-11-16T23:18:05Z")

</div>

Fernando P. wrote:

> Hi,
> 
> I have a Product model. My app creates a fragment cache for  
> [mysite.com/products](http://mysite.com/products), which triggers the index action (my app is  
> RESTful). Only admins can edit products, therefore, when adding a new  
> product, the [mysite.com/products](http://mysite.com/products) should expire.
> 
> But I have a problem: my products administration controller is in  
> admin/products\_controller.rb, and therefore stupid Rails expires  
> admin/products fragment which is not correct.
> 
> Here is part of my code:
> 
> 1. In the view:  
> –  
> \<%- cache(:controller =\> ‘products’, :action =\> ‘index’) do -%\>  
> \<%- for product in @products -%\>  
> …  
> –
> 
> 2. In the admin/products\_controller.rb:  
> –  
> cache\_sweeper :product\_sweeper, :only =\> [:create, :update, :destroy]  
> –
> 
> 3. And my sweeper:  
> –  
> class ProductSweeper \< ActionController::Caching::Sweeper  
> observe Product
> 
> def after\_save(product)  
> expire\_cache(product)  
> end
> 
> def after\_destroy(product)  
> expire\_cache(product)  
> end
> 
> ## def expire\_cache(product) expire\_fragment(:controller =\> ‘products’, :action =\> ‘index’) end end
> 
> How to tell Rails to not be clever, and simply force him to expire  
> products fragment instead of admin/products?

Due to completely outdated information I have been reading on the  
internet, one should replace:

:controller =\> ‘products’, :action =\> ‘index’

by

products\_url

Now caching and expiring works correctly.
