0

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

Community
  • 1
  • 1
Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106
  • try <%= debug @contracten %> at the top of your view - does it output anything? – John Beynon Jul 19 '12 at 07:41
  • when i have the code above it does not work. when i only have

    <%= @contracten.respond_to?(:audits) %>


    in my views and then with the debug code it gives this output: --- ... false
    – Kees Sonnema Jul 19 '12 at 08:29

1 Answers1

0

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 %>
Taryn East
  • 27,486
  • 9
  • 86
  • 108
  • sorry for my late response but this project was for school. but i've tried this and it doesn't show anything now when i add a new record to the database. i don't know why >. – Kees Sonnema Nov 03 '12 at 15:05
  • Yes - this means that "@contracten" is not being set up correctly. There's nothing in it... it's nil. if the "create" method is now not showing anything, the you will need to show us the create method.. but I recommend you first check to be sure that the `@contracten` is actually set up (eg `@contracten = Contracten.create(params[:contracten])`) – Taryn East Nov 05 '12 at 03:22