# Cache\_sweeper causes undefined method error

**URL:** <https://www.ruby-forum.com/t/cache-sweeper-causes-undefined-method-error/53123>\
**Category:** Rails\
**Created:** [January 25, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/cache-sweeper-causes-undefined-method-error/53123 "2006-01-25T22:34:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 25, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/cache-sweeper-causes-undefined-method-error/53123/1 "2006-01-25T22:34:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 25, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/cache-sweeper-causes-undefined-method-error/53123/2 "2006-01-25T22:58:15Z")

</div>

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
