在 ROR 中记录用户活动 [英] Log user activities in ROR

查看:38
本文介绍了在 ROR 中记录用户活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,目前有两种类型的用户,管理员和供应商,我想记录他们的活动,例如

TestAdmin"查看交易TestAdmin"添加了一个新的供应商TestAdmin"编辑了一个交易TestVendor"添加了一个新的交易TestAdmin"批准的交易TestAdmin"拒绝交易等等...

其中TestAdmin"是管理员,TestVendor"是供应商<​​/p>

但我不确定我应该遵循什么方法

我在考虑这个

使用以下字段在数据库中添加活动表

user_id浏览器session_idIP地址行动参数

并打电话

before_filter :record_activity

记录所有活动

但不确定如何创建类似上面的消息

此外,我想弄清楚是否发生任何错误以及何时以及为什么发生,因此为此我使用浏览器字段来确定代码是否在 IE 等中不起作用,但这只是一个错误跟踪字段.

请帮助和指导一些很好的方法来实现这一点.

谢谢

解决方案

好吧,这就是我所做的...

首先创建表

create_table "activity_logs", :force =>真的做 |t|t.string "user_id"t.string "浏览器"t.string "ip_address"t.string 控制器"t.string 动作"t.string "参数"t.string "注释"t.datetime "created_at"t.datetime "updated_at"结尾

在应用程序控制器中创建函数

def record_activity(note)@activity = Activity_Log.new@activity.user = current_user@activity.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结尾

然后从任何需要的控制器调用上面的函数像这样....

class AccountsController <应用控制器load_and_authorize_resource# 发布/帐户# POST/accounts.json定义创建@account = Account.new(params[:account])response_to do |格式|如果@account.saverecord_activity("Created new account") #这将调用应用程序控制器record_activityformat.js { 头:ok }别的format.js { 渲染 json: @account.errors, :error =>真,:成功 =>错误的 }结尾结尾结尾

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

感谢大家的帮助....

I have an application where there are two types of user currently, Admin and Vendor, and i want to log their activities like

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

where "TestAdmin" is an admin and "TestVendor" is a vendor

But i m not sure what approach i should follow

I m thinking of this

Adding an Activity table in DB with following fields

user_id 
browser
session_id
ip_address
action
params

and call

before_filter :record_activity

to record all activities

but not sure how to create messages like above

Also i want to figure out if any error occurs and when and why, so for this i take browser fields to figure out whether code doesn't work in IE etc. but this is only one error track field.

Please help and guide to some nice way to accomplish this.

Thanks

解决方案

Ok this is what i did...

First create table

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

created function in application controller

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

then call above function from any controller needed like this....

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

Hence problem solved......

Thanks all for all their help....

这篇关于在 ROR 中记录用户活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆