0

以下のコードからわかるように。アクションをキャッシュしていshowます。show action には次のメソッドもあります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
4

1 に答える 1

1

views_sweeper ではなく、songs_sweeper を参照する必要があると思います。

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

あなたの要件はよくわかりませんが、SongsSweeper で after_save を after_create に変更することで、より具体的にすることもできます。

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

  def after_create(song)
    expire_fragment(/songs\/#{song.id}\/.*?/)
  end
end
于 2012-01-22T19:28:28.147 に答える