Over a million developers have joined DZone.

Displaying The Last 10 Pages Visited From A Session

·
I wanted to store and display an array of the last 10 visited pages in a session. With the help of some folks in #rubyonrails (particularly aqua) this is what i've come up with:



class ApplicationController < ActionController::Base
	before_filter :add_to_history
	before_filter :page_title
	
	def add_to_history
	  session[:history] ||= []
	  session[:history].unshift ({"url" => @request.request_uri, "name" => page_title })
	  session[:history].pop while session[:history].length > 11
	end


# This bit came from Peter Cooper's snippets source and was moved into the application controller:

	def page_title
		case self.controller_name
			when 'tag' 
				title = "Tags » " + @params[:tags].join(" > ")
			when 'user'
				title = "Users » #{@params[:user]}"	 
			when 'features'
					case self.action_name
						when 'show' then title = "Feature » #{Feature.find(@params[:id]).title}"
						else title = APP_CONFIG["default_title"]
					end
			else 
				title = APP_CONFIG["default_title"] + self.controller_name + ":" + self.action_name
		end
	end
	helper_method :page_title

...
end


In your partial you might do something like this:



User History

<% for cur in session[:history][0..9] -%>

"><%= cur["name"] %>

<% end -%>
Feedback and comments would be appreciated
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}