gpt4 book ai didi

ruby-on-rails - before_filter 与设计

转载 作者:行者123 更新时间:2023-12-04 02:00:37 26 4
gpt4 key购买 nike

我正在使用 Devise 的内置 before_filter :authenticate_user! .如果用户未通过 before 过滤器(尝试在注销时执行操作),我想在我的应用程序助手中调用我自己的自定义方法。我如何以及在哪里可以做到这一点?

最佳答案

我会在使用 user_signed_in? 的过滤器之前写一个自定义.这将只返回一个 bool 值,而不执行任何 authenticate_user! 的重定向类型操作。做。

所以,你可以像这样写一个 before 过滤器:

before_filter :custom_user_auth
...
def custom_user_auth
unless user_signed_in?
# Do custom stuff, ultimately restricting access to the
# ...protected resource if it needs to be
end
end

请注意,除非该过滤器的内部区域 unless,否则此过滤器不会保护您的资源免受未经授权的用户的侵害。语句重定向或呈现。

关于ruby-on-rails - before_filter 与设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6034410/

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