application.rb
%w( observers sweepers ).each do |dir|
config.autoload_paths << "#{Rails.root}/app/#{dir}"
end
development.rb
config.action_controller.perform_caching = true
app/sweepers下 category_sweeper.rb
class CategorySweeper < ActionController::Caching::Sweeper
observe Category
def after_create(category)
puts "enter after_create now..."
expire_cache_for(category)
end
def after_update(category)
expire_cache_for(category)
end
def after_destroy(category)
expire_cache_for(category)
end
private
def expire_cache_for(category)
expire_fragment("side_user_categories_#{category.user.id}")
end
end
app/views/shared/_side_user.html.slim
- cache("side_user_categories_#{@user.id}") do
- categories_ = user_categories
- categories_.each do |category|
= link_to category.name, category
代码如上,现在问题是:当我对 Category 进行 create,update,destroy 操作时,Sweeper 里的那些方法(after_create,after_update,after_destroy)没有被调用,方法没进去。 求解,感谢!