使用 Devise 成功登录后,我可以执行自定义操作吗? [英] Can I execute custom actions after successful sign in with Devise?
问题描述
我有一个具有基本设计身份验证的应用程序.登录后,我想查找用户帐户(用户属于帐户,帐户有_许多用户),并将其存储在会话中,以便它像 @current_user
一样可用.
I have an app that has basic Devise authentication. After sign in, I would like to look up the user account (user belongs_to account, account has_many users), and store that in the session so that it is available like the @current_user
.
像这样在队列中存储会话的 rails 方式是什么?登录成功后,是否有可以与 Devise 一起使用的钩子来执行代码?
What is the rails way of storing session in formation like this? Is there a hook I can use with Devise to execute code after successful sign-in?
推荐答案
Edit:请考虑这曾经是一个很好的解决方案,但可能有更好的方法来处理这个问题.我只是把它留在这里给人们另一种选择并保存历史,请不要投反对票.
Edit: Please consider that this was once a good solution, but there are probably better ways of handling this. I am only leaving it here to give people another option and to preserve history, please do not downvote.
是的,你可以这样做.我要看的第一个资源是 http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in.另外,请查看 如何在使用 rails devise gem 成功注册后重定向到特定页面? 一些想法.
Yes, you can do this. The first resource I'd look at is http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in. Also, check out How to redirect to a specific page on successful sign up using rails devise gem? for some ideas.
您可以执行以下操作:
def after_sign_in_path_for(resource_or_scope)
session[:my_account] = current_user.account
profile_url
end
您可以在 ApplicationController 或自定义 RegistrationsController 中实现此方法.
You can implement this method in your ApplicationController or in a custom RegistrationsController.
这篇关于使用 Devise 成功登录后,我可以执行自定义操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!