gpt4 book ai didi

ruby-on-rails - Rails 3.1 Sweeper 用于不同命名空间中的 Controller 方法

转载 作者:行者123 更新时间:2023-12-04 16:56:32 26 4
gpt4 key购买 nike

全部,

我正在尝试向我的 Rails 3.1 应用程序添加缓存。清扫器位于默认命名空间中,而我的 Controller 位于 Admin 命名空间中。

例如,我在 Admin 命名空间中有 BooksController,每当此 Controller 中的 share 方法时,我都希望清除书籍缓存。我试图将这个方法命名为 after_books_share,但没有调用该方法。

class Admin::BooksController < ApplicationController
caches_action :show
cache_sweeper :book_sweeper
def share
# "Share" a book
end
end


class BookSweeper < ActionController::Caching::Sweeper
observe Book
def after_update(book)
expire_cache_for(book)
end
def after_books_share
book = Book.find params[:id]
expire_cache_for(book)
end
def expire_cache_for(book)
expire_action(:controller => '/books', :action => 'show', :id => book)
end
end

最佳答案

在 Controller 名称前使用斜杠。在默认命名空间中过期:

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

在 admin 命名空间中过期:
expire_action(:controller => '/admin/users', :action => 'index')

关于ruby-on-rails - Rails 3.1 Sweeper 用于不同命名空间中的 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10793103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com