Displaying the last 10 pages visited from a session
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 » " + @params[:tags].join(" > ") 20 when 'user' 21 title = "Users » #{@params[:user]}" 22 when 'features' 23 case self.action_name 24 when 'show' then title = "Feature » #{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