2012-03-23 160 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和關聯。