Rails redirect user to the previous page after signup or login

codesalley

Code Salley

Posted on August 11, 2021

Rails redirect user to the previous page after signup or login

Let's hit the ball, After working on a subscription platform for a while I realized new user's who wanted to pay for service's on the go have to sign up but after signup, Devise redirect the user to the root page, user's will have to look up what they wanted all over again.

devise allows us to override most of the implementation,
we can create a registration controller.

class Users::RegistrationsController < Devise::RegistrationsController



 protected


  def after_sign_up_path_for(resource)
     url = session[:fall_back_url]
       session[:fall_back_url] = nil
      if current_user && url
        url
      end
  end
end
Enter fullscreen mode Exit fullscreen mode

And inside controller methods we might what to save user's url, we'll save it an session fall_back_url

def show
    if !current_user
      session[:fall_back_url] = request.url
    end
end

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
codesalley
Code Salley

Posted on August 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related