ruby-on-railsruby-on-rails-4devisepaper-trail-gem

Rails Papertrail - display who Was Responsible for last change


I'm using Devise and Papertrail and would like to display which user made the most recent update (documentation link).

In controller:

  def show
    # @history = @person.versions.last 
    @last_change = @person.versions.last
    @user_who_made_the_change = User.find @last_change.whodunnit.to_i
  end

In show page

 <%= @user_who_made_the_change %>

However I get the resulting error: undefined method `whodunnit' for nil:NilClass

in app/controllers/people_controller.rb:15:in `show'

Any help would be much appreciated, thanks


Solution

  • There are many reasons for omit save version data (configuration, save without callbacks, save from console ...)

    I think you must check if you have the information to show:

    def show
      @last_change = @person.versions.last
      @user_who_made_the_change = (User.find(@last_change.whodunnit.to_i) if @last_change && @last_change.whodunnit) || nil
    end
    

    This must do the trick.