如何使具有翻译的视图的缓存无效?

想象一下,您有两个视图,其代码如下:

controller_a / a.html.erb

   

controller_b / b.html.erb

        

因此, a.html.erb位于controller_a a.html.erb上,而b.html.erb位于controller_b#b上。 这两个操作都由caches_action缓存。 如何确保在更改some.key转换键时,两个视图都无效? 我怎样才能构建通用机制?

比如,在ApplicationController创建以下类方法(或在lib中并通过它extend ):

 def self.i18n_digest(*scopes) Digest::MD5.hexdigest I18n.t(scopes).to_s end 

然后你可以在你的caches_action使用:cache_path选项:

 caches_action :some_action, cache_path: { some_key: i18n_digest('some', 'foo') } 

只需确保在此语句之前将语言环境设置在before_filter

cache_path文档 。

注意:我正在使用翻译范围( 'some' )将所有嵌套消息作为哈希。