Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

michael www.influxor.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

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:

   1  
   2  
   3  class ApplicationController < ActionController::Base
   4  	before_filter :add_to_history
   5  	before_filter :page_title
   6  	
   7  	def add_to_history
   8  	  session[:history] ||= []
   9  	  session[:history].unshift ({"url" => @request.request_uri, "name" => page_title })
  10  	  session[:history].pop while session[:history].length > 11
  11  	end
  12  
  13  
  14  # This bit came from Peter Cooper's snippets source and was moved into the application controller:
  15  
  16  	def page_title
  17  		case self.controller_name
  18  			when 'tag' 
  19  				title = "Tags &raquo; " + @params[:tags].join(" &gt; ")
  20  			when 'user'
  21  				title = "Users &raquo; #{@params[:user]}"	 
  22  			when 'features'
  23  					case self.action_name
  24  						when 'show' then title = "Feature &raquo; #{Feature.find(@params[:id]).title}"
  25  						else title = APP_CONFIG["default_title"]
  26  					end
  27  			else 
  28  				title = APP_CONFIG["default_title"] + self.controller_name + ":" + self.action_name
  29  		end
  30  	end
  31  	helper_method :page_title
  32  
  33  ...
  34  end


In your partial you might do something like this:

   1  
   2  
   3  <h4>User History</h4>
   4  <% for cur in session[:history][0..9] -%>
   5    <p><a href="<%= cur["url"] %>"><%= cur["name"] %></a></p>
   6  <% end -%>
   7  


Feedback and comments would be appreciated
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS