2012-01-20 4 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 
関連する問題