Rails 3.1 Sweeper for controller method in different namespace

450 views Asked by At

All,

I am trying to add caching to my Rails 3.1 apps. The sweeper lives in default namespace, and I have controller that lives in the Admin namespace.

For example, I have BooksController in the Admin namespace, and whenever the share method in this controller I want book cache to be sweeped. I tried to name this method after_books_share, but the method is not called.

class Admin::BooksController < ApplicationController
caches_action :show
cache_sweeper :book_sweeper
   def share
   # "Share" a book
   end
end


class BookSweeper < ActionController::Caching::Sweeper
observe Book
def after_update(book)
   expire_cache_for(book)
end
def after_books_share
  book = Book.find params[:id]
  expire_cache_for(book)
end
def expire_cache_for(book)
   expire_action(:controller => '/books', :action => 'show', :id => book)
end
end
1

There are 1 answers

1
rogerkk On

Use slashes in front of the controller name. To expire in the default namespace:

expire_action(:controller => '/users', :action => 'index')

To expire in the admin namespace:

expire_action(:controller => '/admin/users', :action => 'index')