11 Ağustos 2007 Cumartesi

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 &raquo; " + @params[:tags].join(" &gt; ")
when 'user'
title = "Users &raquo; #{@params[:user]}"
when 'features'
case self.action_name
when 'show' then title = "Feature &raquo; #{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:
<h4>User History</h4>
<% for cur in session[:history][0..9] -%>
<p><a href="<%= cur["url"] %>"><%= cur["name"] %></a></p>
<% end -%>


0 Comments: