Log user activities in ROR

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


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!