0

In rails how do I remember which page user left at with user login.

When same user comes back he lands on the same page.

I am new to rails not sure how I can approach this problem.

Is there any gem to implement this ?

Thanks

1
  • 1
    You don't? It's a web page and not a native app and your users might not want or expect it to work that way. If you really do you want to read up on cookies.
    – max
    Commented Jul 8 at 9:38

1 Answer 1

0

You can store the last page a user visited in a cookie. Cookies are sent in each request from the browser.

  1. A user navigates to some page of your website for the first time
  2. On the Rails side store the path a user accessed in a cookie (let's say last_visited_path)
  3. Next time a user accesses your website, read the cookie value which should not be empty, and redirect a user to that path.

The problem here is how you decide if you should redirect a user or allow them to visit the requested page. Because you probably only want to redirect them when they just access your website via link, but once they are already on your website you should allow them to navigate to the page they want instead of redirecting them always to 1 page. To solve that you could check the Referrer header: if it's present and it's your website domain (i.e. the request was made from your website) then don't redirect, otherwise redirect.

Not the answer you're looking for? Browse other questions tagged or ask your own question.