Devise redirect back to the original location after sign in or sign up?

前端 未结 8 2246
野趣味
野趣味 2020-12-14 07:42

Here I\'m using Devise Gem for authentication. If someone want to open page without login then it redirect to sign_in page and after signed in it back to the page which user

8条回答
  •  北海茫月
    2020-12-14 08:27

    As pointed out in the official documentation, the simpler solution would be to simply add this to your application_controller.rb:

    class ApplicationController < ActionController::Base
    
    
    private
    
    # If your model is called User
    def after_sign_in_path_for(resource)
      session["user_return_to"] || root_path
    end
    

    Important Note (Which I also overlooked) is that for this to work you will need to call authenticate_user! method, available by default in Devise, in your controller's before_action:. This will call store_location_for available out of the box in Devise, and the rest is handled by the above code in the application_controller.rb, thus eliminating the need to rewrite code to save requesting url.

提交回复
热议问题