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
\"T
I have to say the answer of @Anil D is obviously coupled. It's OK for small projects, but it will cause serious problems (developing, debugging, maintaining problems) as your projects grows bigger and bigger.
I prefer audited: (https://github.com/collectiveidea/audited), which is a simple & clean solution aiming to user activities only. It supports Rails3 and associations.
There are couple of plugins in rails to implement this kind of functionality model-wise. I used acts_as_audited which fulfilled my requirement.
I got to know about one more record_activities, but don't know more about it. Hope it may help to you!
I think you should set model function where it catches the action like after_save
,after update
and all , if your model is Transaction for the logs table fields can be
user_id,
model_name,
action,
ip_address,
session_id,
params
I would suggest you to have these fields on your "Activity" 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
and on your "Activity Model" add a function
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
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....