Rails 3 - logging in a user automatically after creating the account
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 c开发者_StackOverflow中文版ommon 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.
精彩评论