bradpotts bradpotts - 2 years ago 79
Ruby Question

paper_trail show log of changes to a member (no method for each error)

I'm trying to show a log for changes on a particular member in the show view. The members information is there and the version history is working.

I'm having troubles showing versions/changes log for that particular member. I get this error:

ActionView::Template::Error (undefined method `each' for #<PaperTrail::Version:0x007f55f23ee348>):

I have this in my controller:

# Detailed Member Profile
def show
@versions = PaperTrail::Version.find(params[:id])
@members_main = Members::Main.find(params[:id])

And this in my show view:

<div class="row">
<div class="col-md-6">

<div class="col-md-6">
<% @versions.each do |version| %>
<%= l(version.created_at, format: "%-d.%m.%Y %H:%M:%S %Z") %><br/>
Event ID: <%= %><br/>
<b>Target:</b> <%= version.item_type %>
<small>(id: <%= version.item_id %>)</small>; <b>action</b> <%= version.event %>;<br/>
More info:
<pre><%= version.object %></pre>
<% end %>


class Members::Main < ActiveRecord::Base

# Add Paper Trail


Answer Source

I think the find method of the ActiveRecord returns only one object. And that object does not have an each method.

See the documentation here.

In particular this line @versions = PaperTrail::Version.find(params[:id]). It returns an object for the given id. But that object does not have a method called each. That is your error text.

What you probably expected was that it returns an array, but it obviously did not.

Try to change it to @versions = PaperTrail::Version.find([params[:id]]) and I believe it should work. Note the brackets.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download