问题描述
我正在使用Devise的内置 before_filter:authenticate_user!
。如果用户失败了之前的过滤器(尝试在注销时尝试执行一个操作),我想在我的应用程序助手中调用自己的自定义方法。如何和在哪里可以做到这一点?
我将在使用 user_signed_in的过滤器之前编写一个自定义
。这将返回一个布尔值,而不执行 authenticate_user!
的任何重定向类型的操作。
所以,你可以这样写一个过滤器:
before_filter:custom_user_auth
请注意,此过滤器之前不会保护您的资源免遭未经授权的用户,除非该
...
def custom_user_auth
除非user_signed_in?
#做自定义的东西,最终限制访问
#...受保护的资源,如果它需要
end
end
的内部区域除非
语句重定向或呈现。I'm using Devise's built in
before_filter :authenticate_user!
. I want to call my own custom method in my application helper if a user fails the before filter (tries to do an action when logged out). How and where can I do this?解决方案I would write a custom before filter that uses
user_signed_in?
. This will just return a boolean, and not do any of the redirect-type actions thatauthenticate_user!
does.So, you could write a before filter like this:
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
Do note that this before filter won't protect your resource from unauthorized users unless the inside area of that
unless
statement redirects or renders.这篇关于before_filter与设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!