Log user activities in ROR

后端 未结 5 1407
慢半拍i
慢半拍i 2021-01-30 23:25

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         


        
5条回答
  •  Happy的楠姐
    2021-01-31 00:22

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

提交回复
热议问题