I think your sweeper should look like this:
class ProductSweeper < ActionController::Caching::Sweeper observe Product def after_save(product) expire_cache(product) end def after_destroy(product) expire_cache(product) end private def expire_cache(product) expire_fragment('all_available_products') expire_page(:controller => 'products', :action => 'index') end
after_index not a callback unless you define it.
In the controller, you must specify the actions in which the sweeper should be started, in rest order, these actions should be create, update, destroy , so your controller declaration should look like this:
class ProductsController < ActionController caches_action :index cache_sweeper :product_sweeper, :only => [:create, :update, :destroy] def index @products = Product.all end def create @product = Product.new(params[:product]) if @product.save # triggers the sweeper. # do something else # do something else end end # update and stuff ... end
Hope this helps you!
source share