2012-01-20 3 views
0

아래 코드에서 알 수 있습니다. show 작업을 캐싱 중입니다. 나는 또한 쇼 동작에 다음과 같은 방법을 가지고있다 View.create_for(@song).이 시나리오에서 레일 스위퍼를 호출하는 방법은 무엇입니까?

View.create_for(@song)을 호출하면 해당 캐시가 지워집니다.

어떻게하면됩니까? View 모델에서 레일 스위퍼를 수동으로 호출해야합니까? 그렇다면 어떻게?

내 컨트롤러 :

class SongsController < ApplicationController 
    caches_action :show, :cache_path => (proc do 
    song_path(params[:id], :user_id => user_signed_in? ? current_user.id : nil) 
    end) 

    # I tried with the following line, but this is not what I want. I only want to call the sweeper when `View.create_for(@song)` is called: 
    # cache_sweeper :views_sweeper, :only => [:show] 

    def show 
    @song = Song.find(params[:id]) 
    View.create_for(@song) 
    end 
end 

내 스위퍼 :

class SongsSweeper < ActionController::Caching::Sweeper 
    observe Song 

    def after_save(song) 
    expire_fragment(/songs\/#{song.id}\/.*?/) 
    end 
end 

답변

1

난 당신이 songs_sweeper를 참조해야한다고 생각이 아닌 views_sweeper :

cache_sweeper :songs_sweeper, :only => [:show] 

잘 모르겠어요 aftersave를 after_crea로 변경하여 SongsSweeper에서보다 구체적으로 지정할 수 있습니다. te :

class SongsSweeper < ActionController::Caching::Sweeper 
    observe Song 

    def after_create(song) 
    expire_fragment(/songs\/#{song.id}\/.*?/) 
    end 
end 
관련 문제