Forum: Ruby on Rails cache_sweeper causes undefined method error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
csn (Guest)
on 2006-01-25 23:34
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:                      <H1 id="item_title"><%=h @item.title %></H1>

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
csn (Guest)
on 2006-01-25 23:58
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
This topic is locked and can not be replied to.