gpt4 book ai didi

ruby-on-rails - 如何对所有 Controller 使用around_action,但具有某些操作的特定 Controller 除外

转载 作者:行者123 更新时间:2023-12-03 15:59:28 25 4
gpt4 key购买 nike

在我的应用程序 Controller 中,我使用 around_action 在每个 Controller 操作中设置时区:

class ApplicationController < ActionController::Base
around_action :user_time_zone

def user_time_zone(&block)
Time.use_zone("Central America", &block)
end
end

但是在特定的 Controller 中,我不会通过某些操作应用此时区,所以我想要这样的东西:
around_action :user_time_zone, except: {controller: "foo" & action: "bar" & action: "other_action"}

有没有办法做到这一点?

最佳答案

在不需要此过滤器的特定 Controller 中,只需使用“skip_around_filter”跳过它

class FooController < ApplicationController
skip_around_filter :user_time_zone, :only => [:bar]

end

注意:Rails 3.2.1 或更高版本支持skip_around_filter

关于ruby-on-rails - 如何对所有 Controller 使用around_action,但具有某些操作的特定 Controller 除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20715558/

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