成功登录Devise后,我可以执行自定义操作吗? [英] Can I execute custom actions after successful sign in with Devise?

查看:120
本文介绍了成功登录Devise后,我可以执行自定义操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有基本的Devise身份验证的应用程序。登录后,我想查找用户帐户(用户属于_帐户,帐户has_many用户),并将其存储在会话中,以便它可以像 @current_user



这样存储会话的rails方式是什么?
成功登录后,我可以使用Devise执行代码吗?

解决方案

编辑:请考虑这是一个很好的解决方案,但可能有更好的处理方法。我只是留在这里给别人另一个选择,并保存历史,请不要downvote。



是的,你可以这样做。我会看到的第一个资源是 http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in 。另外,请查看如何重定向到成功注册的特定页面使用rails devise gem?了解一些想法。



您可以执行以下操作:

  def after_sign_in_path_for(resource_or_scope)
会话[:my_account] = current_user.account
profile_url
end

您可以在ApplicationController或自定义注册控制器中实现此方法。


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.

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: 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.

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.

You can do something like:

def after_sign_in_path_for(resource_or_scope)
  session[:my_account] = current_user.account
  profile_url
end

You can implement this method in your ApplicationController or in a custom RegistrationsController.

这篇关于成功登录Devise后,我可以执行自定义操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆