rails caching:另一个命名空间中的expire_action

mko*_*kon 16 caching namespaces ruby-on-rails-3

我的应用程序使用命名空间进行管理.我最近尝试开始使用动作缓存,但是我遇到了一些尝试使用expire_action使缓存失效的问题.基本上我在我的默认命名空间newsposts控制器中有一个索引操作,它使用动作缓存进行缓存,如下所示:

class NewspostsController < ApplicationController

  caches_action :index, :layout => false

  def index
    @posts = Newspost.includes(:author).order("created_at DESC").limit(5)
  end

end
Run Code Online (Sandbox Code Playgroud)

这会缓存views/host/newsposts下的视图.

默认命名空间没有修改数据的操作,它们都在我的管理命名空间中.在我的Admin :: NewspostsController中,我试图在create动作中使这个缓存过期,如下所示:

expire_action(:controller => 'newsposts', :action => 'index')
Run Code Online (Sandbox Code Playgroud)

但是这将使位于views/host/admin/newsposts下的缓存文件失效.显然它无法工作,因为我在管理员命名空间和rails(正确地)寻找到此命名空间的缓存过期.遗憾的是,我无法将命名空间参数传递给axpire_action方法,因此如何在另一个命名空间中使操作缓存失效?

mko*_*kon 41

经过一番挖掘,我终于找到了解决方案.在url_for方法中有点暗示:

特别是,前导斜杠确保不会假定名称空间.因此,虽然url_for:controller =>'users'可以解析为Admin :: UsersController,如果当前控制器位于该模块下,url_for:controller =>'/ users'确保您链接到:: UsersController无论如何.

基本上,

expire_action(:controller => '/newsposts', :action => 'index')
Run Code Online (Sandbox Code Playgroud)

将在默认命名空间中到期,并且

expire_action(:controller => 'admin/newsposts', :action => 'index')
Run Code Online (Sandbox Code Playgroud)

在admin命名空间中(默认情况下).

RailsCast