2012-03-23 161 views
14

我这里有两种类型的用户目前,管理和供应商的应用程序,我想记录自己的活动,如登录用户的活动

"TestAdmin" viewed transaction 
"TestAdmin" added a new vendor 
"TestAdmin" edited a Transaction 
"TestVendor" added a new Transaction 
"TestAdmin" approved Transaction 
"TestAdmin" rejected Transaction 
etc... 

其中“testadmin的账户”是管理员,在“TestVendor “是供应商

但是我不知道用什么办法,我应该遵循

我米这种思维

添加在DB的活动表如下字段

user_id 
browser 
session_id 
ip_address 
action 
params 

,并呼吁

before_filter :record_activity 

记录所有活动

,但不知道如何像上面

我也想弄清楚,如果出现任何错误创建的消息以及何时为什么,所以为此我需要浏览器字段来确定代码是否在IE等不起作用,但这只是一个错误跟踪字段。

请帮助和引导一些很好的方法来完成此操作。

谢谢

回答

21

确定这是我做过什么......

首先创建表

create_table "activity_logs", :force => true do |t| 
    t.string "user_id" 
    t.string "browser" 
    t.string "ip_address" 
    t.string "controller" 
    t.string "action" 
    t.string "params" 
    t.string "note" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 
在应用控制器

创建功能

def record_activity(note) 
    @activity = Activity_Log.new 
    @activity.user = current_user 
    @activity.note = note 
    @activity.browser = request.env['HTTP_USER_AGENT'] 
    @activity.ip_address = request.env['REMOTE_ADDR'] 
    @activity.controller = controller_name 
    @activity.action = action_name 
    @activity.params = params.inspect 
    @activity.save 
end 

然后需要 任何像这样的控制器拨打以上功能。 ...

class AccountsController < ApplicationController 
     load_and_authorize_resource 

     # POST /accounts 
     # POST /accounts.json 
    def create 
     @account = Account.new(params[:account]) 
     respond_to do |format| 
     if @account.save 
     record_activity("Created new account") #This will call application controller record_activity 
     format.js { head :ok } 
     else 
     format.js { render json: @account.errors, :error => true, :success => false } 
     end 
    end 
    end 

因此问题解决了......

感谢大家的帮助....

+0

我喜欢这个解决方案。好主意,谢谢。我还添加了一个对象字段,并将对象'to_json'序列化,以便可以看到状态更改以及查看已销毁的项目! – 2012-11-01 13:50:45

+4

我不得不说它显然是耦合的。对于小型项目来说没问题,但随着项目越来越大,它会造成严重的问题。 – 2013-01-06 02:42:14

+0

那么,之后你如何看待活动?你是否使用了特定的图表。 另外,在您的活动中添加浏览器类型可能很有用。我使用浏览器宝石来看看 – 2016-03-13 21:09:09

8

有几个插件在轨道上实现这种功能模型明智。我用acts_as_audited满足了我的要求。

我已经了解了一个record_activities,但不知道更多关于它。希望它对你有帮助!

+0

我用acts_as_audited和编辑的用户信息时,它保存了用户很好的模态数据,但编辑事务,当它不保存在audtis任何事情表。在两个模态类中,我包括“acts_as_audited:protect => false”,任何diea?还有如何在非宁静的控制器中使用它? – 2012-03-23 09:43:55

+0

实际上我想记录审计表中的每个操作,而不是创建和更新,例如“Admin view Transaction”等。 – 2012-03-23 10:03:41

+0

使用acts_as_audited,然后在控制器中使用过滤器将自己的记录添加到acts_as_audited的审计线索。 – 2012-03-29 08:10:05

1

我想你应该设置模式功能,其中它捕获像after_saveafter update动作和所有,如果你的模型是交易的记录表中​​的字段可以

user_id, 
model_name, 
action, 
ip_address, 
session_id, 
params 
+0

如果您只想跟踪更改,这将工作正常,但如果您想跟踪用户查看的记录,它将无法工作。 – 2012-12-12 17:36:38

1

我建议你拥有这些领域在你的 “活动” DB:

model = name of the model ex. "Vendor", "Transaction" 
user_id = id of user 
belongsTo = id of user that own the action/created the action etc.. 
action = name of the action 

,并在你的 “活动型” 增加一个功能

log_activity(model, user_id, belongsTo, action) 
log = Activity.new 
log.model = model 
log.user_id = user_id 
log.belongsTo = belongsTo 
log.action = action 
log.save 
end 

then in you other models that you want to log add callbacks like: 
before_create :log 
before_update :log 
before_destroy :log 


def log 
    Activity.log_activity(self.class, self.user_id, self.belongsTo, self.action) 
    # assuming you have this fields on you forms 
end 
0

我不得不说,@Anil D的答案显然是耦合的。对于小型项目来说没问题,但是随着项目越来越大,它会造成严重的问题(开发,调试,维护问题)。

我更喜欢审计:(https://github.com/collectiveidea/audited),这是一个简单的&干净的解决方案,仅针对用户活动。它支持Rails3和关联。