DEV Community

Code Salley
Code Salley

Posted on

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

Top comments (0)