Cache_sweeper causes undefined method error


#1

I created a sweeper ItemSweeper and saved it in
app/models/item_sweeper.rb. Then I put this in my item_controller.rb:

cache_sweeper :item_sweeper, :only => [:create, :destroy, :update]

But now when I call the view action, I get this error:

ActionView::TemplateError (undefined method `title’ for nil:NilClass) on
line #5 of app/views/item/view.rhtml:
5:

<%=h @item.title %>

It appears that @item isn’t getting retrieved for the view. If I comment
out the cache_sweeper line, it works fine. My first guess is that
perhaps the item sweeper isn’t getting loaded correctly - do I have its
file and class names correct? I don’t see anything in the log related to
the sweeper.

Here’s the sweeper class:

class ItemSweeper < ActionController::Caching::Sweeper

observe Item

def after_create(item)
expire_index item
end

def after_destroy(item)
expire_index item
end

def after_update(item)
expire_index item
end

private

def expire_index(item)
expire_fragment “#index_new_items_type#{item.type_id}”
end

end

thanks for any help!
csn


#2

Ack, turns out item_sweeper.rb got saved to the wrong directory. But
shouldn’t Rails give a helpful error message that it couldn’t load it?

csn