Rails缓存:另一个名称空间中的expire_action [英] rails caching: expire_action in another namespace

查看:78
本文介绍了Rails缓存:另一个名称空间中的expire_action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序出于管理目的使用命名空间.我最近尝试开始使用操作缓存,但是在尝试使用expire_action使缓存过期时遇到了一些问题.基本上,我在默认名称空间新闻发布控制器中有一个索引操作,该操作是使用以下操作缓存进行缓存的:

My application is using a namespace for administrative purposes. I recently tried to start using action caching however I ran into some problems trying to expire the cache using expire_action. Basically I have a index action in my default namespace newsposts controller that is cached using action caching like this:

class NewspostsController < ApplicationController

  caches_action :index, :layout => false

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

end

这会将视图缓存在views/host/newposts下.

This caches the view under views/host/newsposts.

默认名称空间没有用于修改数据的操作,它们都在我的管理员名称空间中.在我的Admin :: NewspostsController中,我试图在create动作中使此缓存过期:

The default namespace has no actions for modifying data, they are all in my admin namespace. In my Admin::NewspostsController I am trying to expire this cache in the create action like this:

expire_action(:controller => 'newsposts', :action => 'index')

但是,这将使位于views/host/admin/newsposts下的缓存文件过期.显然,它不起作用,因为admin名称空间中的im和rails(理所当然地)希望使该名称空间的缓存过期.可悲的是,我无法将名称空间参数传递给axpire_action方法,那么如何在另一个名称空间中使操作缓存过期?

however this will expire a cache file located under views/host/admin/newsposts. Obviously it can not work since im in the admin namespace and rails is (rightfully) looking to expire cache for this namespace. Sadly I can not pass a namespace parameter to the axpire_action method, so how can i expire the action cache in another namespace?

推荐答案

经过更多的挖掘,我终于找到了解决方案. url_for方法中有一些提示:

after some more digging I finally found the solution. It's a bit hinted in the url_for method:

尤其是,斜杠确保不假定任何名称空间.因此,如果当前控制器位于该模块下,则url_for:controller =>'用户'可以解析为Admin :: UsersController,而url_for:controller =>'/users'确保无论如何都链接到:: UsersController.

In particular, a leading slash ensures no namespace is assumed. Thus, while url_for :controller => 'users' may resolve to Admin::UsersController if the current controller lives under that module, url_for :controller => '/users' ensures you link to ::UsersController no matter what.

基本上,

expire_action(:controller => '/newsposts', :action => 'index')

将在默认名称空间中过期,并且

Will expire in the default namespace, and

expire_action(:controller => 'admin/newsposts', :action => 'index')

在管理名称空间中(默认情况下).

in the admin namespace (when in default).

RailsCast

这篇关于Rails缓存:另一个名称空间中的expire_action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆