2011-02-22 41 views
16

我的應用程序正在使用命名空間用於管理目的。我最近試圖開始使用動作緩存,但是我遇到了一些問題,試圖使用expire_action過期緩存。基本上,我有我的默認命名空間newsposts控制器索引操作正在使用動作緩存這樣的緩存:rails緩存:在另一個命名空間中的expire_action

class NewspostsController < ApplicationController 

    caches_action :index, :layout => false 

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

end 

這緩存下的意見/主機/ newsposts的看法。

默認命名空間沒有修改數據的操作,它們都在我的管理命名空間中。在我聯繫:: NewspostsController我想在這樣的創建操作到期此緩存:

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

然而,這將到期坐落在景色/主機/管理/ newsposts一個緩存文件。顯然,它不能工作,因爲im在管理員名稱空間和軌道(正確)期待這個命名空間的緩存過期。令人遺憾的是,我無法將名稱空間參數傳遞給axpire_action方法,因此如何在另一個名稱空間中將動作緩存過期?

回答

40

經過一番更多的挖掘,我終於找到了解決方案。這在url_for方法中有點暗示:

特別是,前導斜槓確保沒有命名空間。因此,如果當前控制器位於該模塊下,則url_for:controller =>'users'可能會解析爲Admin :: UsersController,但url_for:controller =>'/ users'確保您無論如何鏈接到:: UsersController。

所以基本上,

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

將在默認命名空間中到期,

expire_action(:controller => 'admin/newsposts', :action => 'index') 
在管理命名空間

(默認時)。另外

RailsCast

+1

嘿,謝謝你!很難找到:) – fuzzyalej

0

一個說明我瞭解到,如果要終止特定的格式,如XML,JSON,等等,僅僅

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

或任何你想要的格式。它看起來有一段時間要弄清楚。

相關問題