Rails 3-创建帐户后自动登录用户 [英] Rails 3 - logging in a user automatically after creating the account

查看:63
本文介绍了Rails 3-创建帐户后自动登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的users_controller中,我有两种方法signup_processlogin.我想在成功调用signup_process后自动为用户调用login.

In my users_controller I have two methods signup_process and login. I would like to call login automatically for a user after a successful signup_process call.

这可能吗?在其他语言框架中,我通常只直接通过传递用户名和密码的signup_process动作来调用login控制器动作-但是我认为这在Rails中是不受欢迎的.

Is this possible? In other language frameworks I would usually just call the login controller action direct from the signup_process action passing the username and password - but I think that is frowned upon in Rails.

是否有其他方法可以将用户数据从我的控制器发布users.login操作?

Is there some way instead to post the user data to the users.login action from my controller?

这必须是一种常见的模式-我想念什么? :)

This must be a common pattern - what am I missing? :)

提前谢谢!

推荐答案

不确定皱眉的意思,但这是一种方法

Not sure what you mean by frowned upon, but here's one way

class UsersController < ...
   def signup
      // Do some stuff
      do_login(username, password)
      // render or redirect as you wish
   end

   def login
     do_login(username,password)
     // render or redirect as you wish
   end

   private
   def do_login(username,password)
     // do the actual login processing 
     // can even render or redirect here if it's common to both setup and login
   end

end

那你想做什么吗?

这篇关于Rails 3-创建帐户后自动登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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