微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – Rails caches_action正在跳过before_filters

我有一个控制器,我正在缓存展示动作.显示操作有一些以前的安全性过滤器,用于执行并重定向,如果用户登录,而不是当前组的成员等.这些过滤器在我没有开启缓存时工作正常,但是当我翻转开关以在之前的过滤器不再执行(我的调试器调用没有命中)的缓存.

一直以来我的理解是,在过滤器被称为缓存操作之前,这是页面缓存和动作缓存之间的主要区别.这是由Rails Caching Tutorial section on action caching支持,内容如下:

Action Caching works like Page Caching except for the fact that the incoming web request does go from the webserver to the Rails stack and Action Pack so that before filters can be run on it before the cache is served. This allows authentication and other restriction to be run while still serving the result of the output from a cached copy.

那么为什么我以前的过滤器被调用

有关我的设置:Rails 3.1使用Devise进行身份验证.我正在使用dalli宝石作为一个memcached商店.

这是一些代码,总结了我的代码(很多cruft切出):

class GroupsController < ApplicationController
  caches_action :show
  cache_sweeper :group_sweeper

  before_filter :authenticate_user!,:except => [:index]
  before_filter :init_group,:except => [:new,:create,:index]
  before_filter :requires_group_membership,:index]

  def show
  end

  private

  def requires_group_membership
    if current_user and !@group.users_active.index(current_user).nil?
      return true
    else
      redirect_to :root
      return false
    end
  end

  def init_group
    @group = current_user.active_groups.find_by_id(params[:id])

    if @group.nil?
      redirect_to :root
      return false 
    end
  end

那么以前有谁看过这个行为吗?我有一个洞,我理解过滤器和动作缓存应该如何工作?或者也许我有一些奇怪的voodo发生与奇怪的宝石版本组合?

[编辑]

有趣的是,我刚刚知道,返回值对于是否运行链条进一步的方法没有影响,是否调用重定向或渲染.

[编辑2]

升级了我的应用程序到轨道3.2.3看看它是否有影响,但没有解决问题.我发现的一些事情是ApplicationController中定义的以前的过滤器被调用,但是我的GroupsController中没有.

解决方法

这是一个非常时间的方式来学习关于缓存的新tidbit.

事实证明,您必须在要运行的before_filters之前调用caches_action.我将缓存操作作为我课程中的第一件事情之一,所以以前的所有过滤器都没有像下面的那样运行.

感谢Pan Thomakoshis answer包含这个宝石的信息 – 这不是在红宝石文档或我已经撇去了它.一旦我设法克服了由于这个小盲点造成的时间损失,我将尽力将这个信息添加到文档中.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐