Railsキャッシング:パラメーターを必要とするアクションにスイーパーを使用する
-
24-10-2019 - |
質問
スイーパーを使用してページのリフレッシュを処理しようとしています。インデックスアクションなどを更新するためには、すべてが正常に機能します...しかし、ページパラメーターを解釈するためにスイーパーのようには見えません。誰かが以下のコードの何が悪いのか教えてくれるなら、私は非常に感謝しています:
コントローラ:
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
解決
コピーをコピーして貼り付けたと仮定した場合、タイプミスもコードにあります。 Railsによるエラーがフラグを立てられなかったため、スイーパーが呼ばれていないと仮定できます。 (つまり、afth_updateは呼び出されていません)。ロガーメッセージを追加して、実際にそうであることを確認します。
質問は投稿に関するものです:
- それはActiverecord :: Baseの被相続人ですか?
- 虚偽を返してチェーンを停止する他のコールバックはありますか?
ネット上のスイーパーの例は一貫してcache_sweeperラインを置きます 後 caches_xxx行。それが違いをもたらすが、チェックアウトする価値があるなら、私は驚かれるでしょう。
所属していません StackOverflow