Fernando Perez
2008-Nov-16 22:09 UTC
Fragment caching not expiring the correct fragment cache
Hi,
I have a Product model. My app creates a fragment cache for
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 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?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Fernando Perez
2008-Nov-16 22:18 UTC
Re: Fragment caching not expiring the correct fragment cache
Fernando Perez wrote:> Hi, > > I have a Product model. My app creates a fragment cache for > 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 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---