rails 4中的expire_fragment缓存

时间:2015-11-03 12:39:50

标签: ruby-on-rails caching

我在rails 4应用程序中使用片段缓存,我试图使一个尚未缓存的片段到期。这就是我收到错误的原因:

"undefined method `expire_fragment' for #<ItinerarySweeper:0xb5a3510 @controller=nil>"

这是我的sweerp.rb文件:

class ItinerarySweeper < ActionController::Caching::Sweeper
  observe Itinerary

  def after_update(itinerary)
    expire_cache(itinerary)
  end

  def after_destroy(itinerary)   
    expire_cache(itinerary)
  end

  def expire_cache(itinerary)
    expire_fragment "itinerary_percentage_#{itinerary.id}"
    expire_fragment "city_timeline_#{itinerary.id}"
  end
end

这是我的控制器

 class ItinerariesController < ApplicationController
   cache_sweeper :itinerary_sweeper, :only => [:update, :destroy]
   ......
 end

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试改变这个:

def expire_cache(itinerary)
  expire_fragment "itinerary_percentage_#{itinerary.id}"
  expire_fragment "city_timeline_#{itinerary.id}"
end

进入这个:

 def expire_cache(itinerary)
   ActionController::Base.new.expire_fragment("itinerary_percentage_#{itinerary.id}", options = nil)
   ActionController::Base.new.expire_fragment("city_timeline_#{itinerary.id}", options = nil)
 end