2

TL;博士無法on Rails的expire_action

expire_index下面的方法獲取調用,我看到了puts在日誌中。但是,當我刷新頁面是陳舊的版本。

說明:我正在使用rails_admin更新模型。但也注意到直接使用rails控制檯的相同行爲。

感謝您的幫助。非常感激!

細節

app/controllers/posts_controller.rb

class PostsController < ApplicationController 
    caches_action :index 
    cache_sweeper :post_sweeper 

    def index 
    @posts = Post.published 
    end 

    def show 
    @post = Post.find(params[:id]) 
    end 
end 

app/sweepers/post_sweeper.rb

class PostSweeper < ActionController::Caching::Sweeper 
    observe Post 

    def after_save(post) 
    puts "======================" 
    puts "  AFTER SAVE  " 
    puts "======================" 
    expire_index 
    end 

    private 
    def expire_index 
    puts "======================" 
    puts " EXPIRING INDEX  " 
    puts "======================" 
    expire_action(:controller => '/posts', :action => 'index') 
    end 
end 

config/environments/production.rb

config.action_controller.perform_caching = true 
config.cache_store = :dalli_store # using memcachier on heroku 
+0

嘗試沒有 '/' 在控制器選項: expire_action(:控制器=> '帖子',:動作=>「指數') – cthulhu

+0

@cthulhu也不起作用。由於https://github.com/rails/rails/issues/3546,我嘗試使用'/'。 –

回答

3

得到它的工作。這裏是什麼了:這個要點

def expire_index 
    cache_key = "views/#{request.host_with_port}/posts" 
    Rails.cache.delete(cache_key) 
end 

更多細節 - >https://gist.github.com/4400728