Search code examples
htmlruby-on-railscontrollerruby-on-rails-3.2acts-as-audited

Rails 3.2 error showing Audited changes in views


I have a strange problem. My audited gem does not work in my views when i have this code in my contracten view:

<p><%= @contracten.respond_to?(:audits) %></p>
<br />
<%= @contracten.audits.each do |audit| %>
  <%= audit.version %>
  <br />
<% end %>

it gives the following error: undefined methodaudits' for nil:NilClass`

When i only have this line in my views: <p><%= @contracten.respond_to?(:audits) %></p> It showes false instead of true so i don't think my controller is working properly.

My controller show:

  def show
    add_breadcrumb 'Contract Bekijken', :contracten_path

    @contracten = Contracten.find(params[:id])
  end

My model:

class Contracten < ActiveRecord::Base

  audited

  has_attached_file :pdf

  attr_accessible :betalingsperiodeeenheidid, :betalingstermijn, :contractduur, :contractid, :contractsoortid, :datumeinde, :datumingang, :naam, :omschrijving, :opzegtermijn, :organisatieid, :persoonid, :vestigingid, :pdf

end

I posted a similar question here: Show Last modified tables/records Rails 3.2 but that's not specific about audited. i did not find a simplier gem for it so this may work. but i'm still struggling with the views


Solution

  • false is a correct response for nil.respond_to?(:audits)

    your @contracten is not being set up properly and it is nil. You probably need:

    <% if @contracten.present? %>
      <p><%= @contracten.respond_to?(:audits) %></p>  
      <%= @contracten.audits.each do |audit| %>
        <br /><%= audit.version %>
      <% end %>
    <% end %>