How to display a list of “latest pages visited”

后端 未结 2 901
情深已故
情深已故 2021-01-01 03:31

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

I know that I can do redirect_to(request.referer) or redirect_to(:back) to lin

相关标签:
2条回答
  • 2021-01-01 04:04
    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 later you can do

      redirect_to session[:latest_pages_visited].last
    
    0 讨论(0)
  • 2021-01-01 04:10

    You can put something like this in your application_controller:

    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 store the five latest url visited

    0 讨论(0)
提交回复
热议问题