Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log user activities in ROR

Tags:

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

like image 346
Anil D Avatar asked Mar 23 '12 08:03

Anil D


People also ask

What is user activity log?

The User Activity Log will display user activities based on your filter criteria and Activity Group (whether it be Reservation, Posting, Housekeeping, Commission, Configuration, Employee, Profile, Blocks, or Potential, among others).


2 Answers

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....

like image 120
Anil D Avatar answered Sep 28 '22 13:09

Anil D


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!

like image 37
Nimesh Nikum Avatar answered Sep 28 '22 12:09

Nimesh Nikum