2012-10-02 25 views
0

我有一個Rails3應用程序與Article模型。如何做rails3有條件緩存

當管理員登錄後,他們可以通過簡單地將文章/ show方法查看在「未授權」的文章:

if [email protected]_authorized and !user_signed_in? 
    raise ActionController::RoutingError.new('Not Found') 
end 

我想緩存從他們被授權的時刻的文章。當管理員正在審覈他們時,我如何防止他們被緩存?

如何在動作中放置類似以下的代碼?

if @article.is_authorized 
    cache_action 
    or cache_fragment 
    or whatever 
+0

你應該緩存的行動,並在的before_filter – apneadiving

+0

檢查權威性是有這樣的東西作爲條件緩存?我可以根據before_filter中的if-condition選擇是否緩存動作嗎? –

回答

0

As stated in the doc,你可以傳遞一個Proc:

caches_action :foo, if: lambda { your_condition_here } 
+0

引用的「文檔」已更新,不再解釋這一點。 下面是它的API文檔: http://apidock.com/rails/v3.2.13/ActionController/Caching/Actions – Highwaybobbery