bradpotts bradpotts - 5 months ago 11
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])
end


And this in my show view:

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

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


Model:

class Members::Main < ActiveRecord::Base

# Add Paper Trail
has_paper_trail

end

Answer

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.

Comments