我正在尝试使用扫地机来处理我的页面刷新。对于刷新的索引操作等,一切都很好...但是我似乎无法解释页面参数。如果有人能告诉我以下代码有什么问题,我将非常感谢:

控制器:

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的错误标记,因此我们可以假设没有调用扫地机。 (即未调用after_update)。我会添加一些记录器消息,以验证确实如此。

问题是关于帖子的:

  1. 这是Activerecord的死者::基础吗?
  2. 您还有其他回调的返回错误并因此停止链条吗?

网上的扫地示例始终放在cache_weeper系列 caches_xxx行。如果这有所作为,我会感到惊讶,但值得一试。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top