如何显示"访问过的最新页面"列表

tol*_*org 5 ruby-on-rails

在rails中,如何显示当前用户访问过的最近5页的列表?

我知道我可以执行redirect_to(request.referer)或redirect_to(:back)链接到最后一页,但是如何创建实际的页面历史列表?

它主要用于原型,所以我们不必将历史存储在db中.会议将做.

jig*_*fox 11

你可以在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
Run Code Online (Sandbox Code Playgroud)

现在它存储了访问过的五个最新网址


Amo*_*ari 8

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
....
Run Code Online (Sandbox Code Playgroud)

以后你可以做

  redirect_to session[:latest_pages_visited].last
Run Code Online (Sandbox Code Playgroud)