Topic: [SOLVED] sweepers in nested routes

I have a controller called posts where I want to cache index and show pages.

#posts_controller.rb
caches_page :index, :show

I have a sweepers directory with the following.

#post_sweeper.rb
class PostSweeper < ActionController::Caching::Sweeper
  observe Post

  def after_save(post)
    expire_cache(post)
  end

  def after_destroy(post)
    expire_cache(post)
  end

  def expire_cache(post)
    expire_page :controller => 'posts', :action => 'index', :format => 'html'
    expire_page :controller => 'posts', :action => 'show', :id => post.id :format => 'html'
  end
end


Now that normally would work just fine for me. Problem is in this app I am trying to call the sweeper from a nested route, /admin/posts/

#routes.rb
map.namespace :admin do |admin|
  admin.resources :posts
  admin.resources :users
end

#app/controllers/admin/posts_controller.rb
cache_sweeper :post_sweeper, :only => [:create, :update, :destroy]

Never runs the sweeper or its trying to run it for the admin/posts controller where there is no cache. To be completely honest I'm not positive what its doing. What I need to change or how to even debug this. Can someone explain how to set this up for a nested route?

Last edited by gregf (2008-02-05 16:42:59)

Re: [SOLVED] sweepers in nested routes

Ok, I finally got back to trying this after getting really annoyed the last time. Focused on just tracking down what it was trying to delete or not trying to delete rather. After grepping my development log I found out it was trying to delete '/admin/posts.html' which is what I expected was happening before. That made me smile to at least know I could move on to fixing it. Next thing I did ended up solving it, although there maybe a better solution. Like I mentioned in the top post, I cache from controllers/posts_controller.rb, then I call my sweeper method from /admin/posts_controller.rb. My route action in my routes config is 'posts' but I have a namespace setup for admin/posts so I guess it justs confuses it.

So I had this

expire_page :controller => 'posts', :action => 'index', :format => 'html'

And Just added a / in front of the controller now so it now looks like this.

expire_page :controller => '/posts', :action => 'index'

Dropped the format off the end after I found out I don't need it.

Re: [SOLVED] sweepers in nested routes

gregf, that forward slash worked for me too. Thanks for posting and following up.