简体   繁体   English

Rails 3 - 在创建帐户后自动登录用户

[英]Rails 3 - logging in a user automatically after creating the account

In my users_controller I have two methods signup_process and login . 在我users_controller我有两种方法signup_processlogin I would like to call login automatically for a user after a successful signup_process call. 我想在signup_process调用成功后自动为用户调用login

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. 在其他语言框架中,我通常只是通过传递用户名和密码的signup_process操作直接调用login控制器操作 - 但我认为这在Rails中是不受欢迎的。

Is there some way instead to post the user data to the users.login action from my controller? 是否有替代某种方式将用户数据发布users.login从我的控制器行动?

This must be a common pattern - what am I missing? 这必须是一种常见的模式 - 我缺少什么? :) :)

Thanks in advance! 提前致谢!

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

Would that do what you want? 那会做你想要的吗?

Well, what does the login action do? 那么,登录操作有什么作用?

Most likely, it sets something in the session indicating the user is logged in. You could do just that, after creating the user. 最有可能的是,它在会话中设置了指示用户已登录的内容。在创建用户之后,您可以这样做。

It does not make sense to call a controller action, since it's most likely hooked up with a view/form. 调用控制器动作没有意义,因为它最有可能与视图/表单连接。

Please provide more information on what the login action does, if you still feel like you need to go through it. 如果您仍然觉得需要通过它,请提供有关登录操作的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM