Question

Comme vous pouvez le voir sur le code ci-dessous. Je cache le show action. J'ai aussi la méthode suivante dans l'action Show View.create_for(@song).

Je voudrais le faire, quand View.create_for(@song) est appelé, il élimine le cache respectif.

Comment pourrais-je y aller? Dois-je invoquer manuellement le balayeur des rails dans le View maquette? Si c'est le cas, comment?

Mon contrôleur:

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

Ma balayeuse:

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

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

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top