P Shved
P Shved

Reputation: 99254

How to count total number of requests (links) to a site in Rails 3?

I want to add a panel that shows site access stats to a Rails 3 application (as in Google Analytics). A naïve method to do this is to record IP addresses into a designated table with their access times.

Is there a function or a callback that is called at each request? If not, is there any other way I can count the total number of accesses to a Rails 3 application?

Upvotes: 1

Views: 1221

Answers (2)

AlejandroHernandez.Inc
AlejandroHernandez.Inc

Reputation: 153

I know it's old but when someone would like to know:

(total times) = Request.count

Upvotes: 0

user229044
user229044

Reputation: 239240

The "callback" you're looking for is a simple before_filter applied inside your application controller.

class ApplicationController < ActionController::Base

  before_filter :log_request

  protected

  def log_request
    # Write request information to the database or log file
  end 
end

You are correct in calling this a very naive approach though. You could also simply analyze your log files to obtain the same information.

Upvotes: 4

Related Questions