How to display a list of "last visited pages"

On rails, how do I display a list of the last 5 pages the current user has visited?

I know that I can do redirect_to (request.referer) or redirect_to (: back) to link to the last page, but how to create an actual list of page history?

This is basically a prototype, so we do not need to store the history in db. The session will be completed.

+4
source share
2 answers

In the application_controller application, you can add something like this:

class ApplicationController < ActionController::Base before_filter :store_history private def store_history session[:history] ||= [] session[:history].delete_at(0) if session[:history].size >= 5 session[:history] << request.url end end 

Now it stores the last five pages visited.

+9
source
 class ApplicationController < ActionController::Base after_action :set_latest_pages_visited def set_latest_pages_visited return unless request.get? return if request.xhr? session[:latest_pages_visited] ||= [] session[:latest_pages_visited] << request.path_parameters session[:latest_pages_visited].delete_at 0 if session[:latest_pages_visited].size == 6 end .... 

and then you can do

  redirect_to session[:latest_pages_visited].last 
+8
source

Source: https://habr.com/ru/post/1415104/


All Articles