1

我想添加一个面板来显示 Rails 3 应用程序的站点访问统计信息(如在 Google Analytics 中)。一种简单的方法是将 IP 地址及其访问时间记录到指定的表中。

是否有在每个请求时调用的函数或回调?如果没有,还有其他方法可以计算对 Rails 3 应用程序的访问总数吗?

4

2 回答 2

4

您正在寻找的“回调”是before_filter应用程序控制器内部的一个简单应用。

class ApplicationController < ActionController::Base

  before_filter :log_request

  protected

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

不过,您称这是一种非常幼稚的方法是正确的。您也可以简单地分析您的日志文件以获得相同的信息。

于 2012-02-27T18:55:14.050 回答
0

我知道它很旧,但是当有人想知道时:

(总次数)= Request.count

于 2016-11-15T23:39:52.393 回答