From feaea1636c0d14de7e85bb3b81c02cc145c38f61 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 10 Aug 2011 22:02:04 -0400 Subject: [PATCH] futzing with cookies in 15.12, although my cookie counter is getting called twice per request... perhaps the "before_filter" method is applied for each filter in a chain? --- .../app/controllers/application_controller.rb | 35 +++++++++++-------- .../hodgepodge/app/views/index/index.html.erb | 2 ++ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/cookbook/015/hodgepodge/app/controllers/application_controller.rb b/cookbook/015/hodgepodge/app/controllers/application_controller.rb index c21a963..b3b82c4 100644 --- a/cookbook/015/hodgepodge/app/controllers/application_controller.rb +++ b/cookbook/015/hodgepodge/app/controllers/application_controller.rb @@ -1,21 +1,28 @@ class ApplicationController < ActionController::Base - before_filter :set_user + before_filter :set_user, :count_visits protect_from_forgery + private + def count_visits + value = (cookies[:visits] || '0').to_i + cookies[:visits] = (value + 1).to_s + @visits = cookies[:visits] + end + protected - def set_user - @user = User.find(session[:id]) if @user.nil? && session[:id] - end + def set_user + @user = User.find(session[:id]) if @user.nil? && session[:id] + end - def login_required - return true if @user - access_denied - return false - end + def login_required + return true if @user + access_denied + return false + end - def access_denied - session[:return_to] = request.request_uri - flash[:error] = 'Oops. You need to login before you can view that page.' - redirect_to :controller => 'user', :action => 'login' - end + def access_denied + session[:return_to] = request.request_uri + flash[:error] = 'Oops. You need to login before you can view that page.' + redirect_to :controller => 'user', :action => 'login' + end end diff --git a/cookbook/015/hodgepodge/app/views/index/index.html.erb b/cookbook/015/hodgepodge/app/views/index/index.html.erb index d07e59d..a9a9f1f 100644 --- a/cookbook/015/hodgepodge/app/views/index/index.html.erb +++ b/cookbook/015/hodgepodge/app/views/index/index.html.erb @@ -1,2 +1,4 @@

You first visited this site on <%= session[:first_time] %>.

That was <%= time_ago_in_words session[:first_time] %> ago.

+ +

You have visited this website's pages <%= @visits %> time(s).