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

会有一股神秘感。 提交于 2019-12-25 08:57:49

问题


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.

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.

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? :)

Thanks in advance!


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/5916818/rails-3-logging-in-a-user-automatically-after-creating-the-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!