4

In my app that I build to learn RoR, I want to add change tracking and use the Audited gem for that. While it is clear how to add it to my models, how can I see the changes?

I would want to add a link / button on each page for the different objects / classes / models that opens a view with the audit log. i.e. listing all changes (date/time, field, old value, new value, by user) sorted by the date/time of the change.

Can't find any documentation for it. All help to get started appreciated.

Dimitri de Ruiter
  • 725
  • 2
  • 7
  • 26

2 Answers2

6

I was needed exact feature when I was working with Papertrail Gem few months back. I modified my code to work for audited gem. I hope below haml code will give you really nice start.

%table.table.table-hover
  %thead
    %tr
      %th Type
      %th When
      %th Who
      %th What Changed
    - model.audits.order(:created_at).each do |audit|
      %tr
        %td= audit.action
        %td= audit.created_at
        %td= audit.user.name
        %td
          - audit.audited_changes.each do |k, v|
            %b= k.titleize
            from
            %b= "'#{v[0]}'"
            to
            %b= "'#{v[1]}'"
            %br

The code is self explanatory If you go through https://github.com/collectiveidea/audited

Explanation for audited_changes: For audited_changes we have hash like audit.audited_changes # => {"name"=>["Steve", "Ryan"]}. That means you have hash with string as a key and array with two values. first value is before updation and second is after updation.

dnsh
  • 3,516
  • 2
  • 22
  • 47
  • Tx Dinesh - will take a look at it early this week. Then let you know. – Dimitri de Ruiter Oct 08 '16 at 14:59
  • Pay attention: audited_changes value could be an array OR string! - audit.audited_changes.each do |k, v| %b= k.titlesize - if v.class == Array from = "'#{v[0]}'" to =# "'#{v[1]}'" - else = "'#{v}'" %br – dumP Oct 24 '16 at 07:15
  • Why do I get `undefined method 'audits'`? I'm running Ruby 2.5.1, Rails 5.2.0, and Audited 4.8.0. I copied the code then replace `model` with my modal name/class. – dcangulo Sep 07 '18 at 10:14
  • How did you resolve the problem you encountered ? @DavidAngulo – chrisgeeq Jul 22 '19 at 21:36
2

Thanks to @dnsh, i changed his code and added else:

%table.table.table-hover
  %thead
    %tr
      %th Type
      %th When
      %th Who
      %th What Changed
    - model.audits.order(:created_at).each do |audit|
      %tr
        %td= audit.action
        %td= audit.created_at
        %td= audit.user.name
        %td
          - audit.audited_changes.each do |k, v|
            %b= k.titleize
            - if v.class == Array # if updated
              from
              %b= "'#{v[0]}'"
              to
              %b= "'#{v[1]}'"
              %br
            - else # if created
              = v
dumP
  • 771
  • 8
  • 17
  • I am not sure why this answer was upvoted, but it currently doesn't work as the audits method throws up an undefined error message https://stackoverflow.com/questions/27353017/gem-audited-activerecord-4-0-gives-undefined-method-audits – chrisgeeq Jul 27 '19 at 14:34