简体   繁体   中英

Rails/Devise How to redirect to a specific page after login

After login and signup , I want to redirect the user to a specific page only if the user came from a specific page.

What I have tried to do so far – using devise :

  1. I added a parameter to a link and checked that parameter in the login page. Using the method after_sign_in_path_for(resource) from devise , I created a conditional to that parameter. If true , the page would be redirect to my wanted page

    Link

     <%= link_to "Login before continuing", new_user_session_path(:registration_process => true) 

    On ApplicationController.rb

     def after_sign_in_path_for(resource) if params[:registration_process] leads_path # desired page else super end end 

This is not working.

I don't know if using params is the best option, since I want to do the same with the sign up page.

Thank you very much. Any help will be much appreciated.

Easy! Why not try:

URI(request.referer).path ?

Now you can basically identify the path where you came from.

You can also store the page where you came from in a session. eg

session[:intended_url] = request.url

hope that helps.

Try This one

if resource.class == User && current_user
    user_dashboard_path
else
    super
end

我认为您应该在具有new_user_session_path形式的会话中传递带有会话路径的参数,例如url: session_path(resource_name, :registration_process => true) ,然后再使用if params[:registration_process]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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