Cache_sweeper causes undefined method error

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:

<%=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

def after_destroy(item)
expire_index item

def after_update(item)
expire_index item


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


thanks for any help!

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?


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs