How do I redirect a user to a specific page when they login on a particular form I create with Devise? There’s this howto page for Devise on how to redirect to a particular page after sign in. However, I want different forms to sign in to different target pages. Ideally, I can just add another HTML form parameter like this:
<input type="hidden" name="target" value="/dashboard"/>
UPDATE: It’s important that I maintain the normal functionality of Devise for a login page. That is, if I click on a link that has a before_filter :authenticate_user! in its controller, then I’ll be redirected to a login page. After logging in, then I’m redirected to the original destination page.
Actually you can simply define the
after_sign_in_path_formethod in your controller like that :And then if your different sign in forms have different targets, then user should be properly redirected
Adding
to each particular form that would redirect to a specific page will then be enough 🙂
EDIT : I think I finally understood what you exactly want. So here what I would do (based on this blogpost)
So basically, if you try to access a protected path (let’s say
/protected_resource), the path will be stored in the session, and then once user is logged in, the session will be cleared and user correctly redirectedThen if a user goes on one of your different
sign_inform and if that form contains atargetinput, then user will be redirected to that target.Finally, if user goes on a form without any target he’ll be redirected to a
default_redirect_paththat you might want to customize.Last thing : I assumed that the redirection to the original request was more important than the target so let’s say a user goes on
/protected_resourceand is therefore redirected to a sign_in form, even though this sign_in form has atargetinput, user will be redirected to/protected_resourceafter a successful sign in.You can easily change that by inverting the conditions in the
after_sign_in_path_formethodThere might be a better way to do that, but this a starting point for you, and you might want to improve it 😉
Let me know if my answer is not detailed enough.