4

ページの更新を処理するためにスイーパーを使用しようとしています。インデックス アクションの更新などについては、すべて正常に動作します...しかし、スイーパーがページ パラメータを解釈できないようです。以下のコードの何が問題なのか誰か教えていただければ、とても感謝しています。

コントローラ:

class PostsController < ApplicationController
  load_and_authorize_resource
  cache_sweeper :post_sweeper, :only => [:create, :update, :destroy]
  caches_page :index
  caches_page :show
  caches_action :edit, :new

  # This refreshes cache correctly
  def index
    @posts = Post.all
  end

# これによりキャッシュが作成されますが、更新はされません (これまで)。expire_page コマンドを (スイーパーの代わりに) アクションに直接配置すると、正常に動作します

def update
    @post = Post.find(params[:id])
    respond_to do |format|
      if @post.update_attributes(params[:post])
        flash[:notice] = t(:post_updated)
        format.html { redirect_to(@post) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

スイーパー:

class PostSweeper < ActionController::Caching::Sweeper
  observe Post

  def after_create(record)
    expire_cache_for_index(record)
  end

  def after_update(record)
    expire_cache_for_index(record)
    expire_cache_for_post(record)
    expire_cache_for_edit(record)
  end

  def after_destroy(record)
    expire_cache_for_index(record)
    expire_cache_for_post(record)
    expire_cache_for_edit(record)
  end

  private
  def expire_cache_for_index(record)
    expire_page :controller => 'posts', :action => 'index'
  end

  def expire_cache_for_post(record)
    expire_page :controller => 'posts', :action => 'show', :id => record.id
  end

  def expire_case_for_edit(record)
    expire_action :controller => 'posts', :action => 'edit', :id => record.id
  end

end
4

1 に答える 1

1

コードをコピーして貼り付けたと仮定すると、タイプミスはコードにもあります。Rails によってエラーのフラグが立てられなかったので、スイーパーが呼び出されていないと推測できます。(つまり、after_update は呼び出されていません)。実際にそうであることを確認するために、いくつかのロガー メッセージを追加します。

質問は投稿に関するものです:

  1. ActiveRecord::Baseの子孫ですか?
  2. false を返し、チェーンを停止している他のコールバックはありますか?

ネット上のスイーパーの例では、一貫して caches_xxx 行のに cache_sweeper 行が配置されています。それが違いを生むなら私は驚くだろうが、それはチェックする価値がある.

于 2011-09-28T03:53:06.843 に答える