ruby-on-railsrubyruby-on-rails-5paper-trail-gem

PaperTrail whodunnit set to name?


how to set whodunnit to current_user name.

Currently i use of whodunnit id to find the name of particular MODEL

versions.each do |a|
 
  = MODEL.find(a.whodunnit).name

end

here's the problem what if the MODEL that we finding is DELETED. im getting a error

so my GOAL is to save the current_user name in whodunnit. is this possible?


Solution

  • you can do it like:

    # app/controllers/application_controller.rb
    
    class ApplicationController < ActionController::Base
      before_action :set_paper_trail_whodunnit
    
      protected
      
      def user_for_paper_trail
        current_user&.name || "Public User"
      end
    end
    

    Happy Coding :-)