简体   繁体   中英

Using partial with paper trail

I'm trying to put some code in a partial from my show page in /events. The code works fine when I use it in my show page, but when i extract it to a partial I'm getting a No Method Error

undefined method `event' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_PaperTrail_Version:0x007f4368823a68>

versions/_version.html.erb

<% cache version do %>
  <div class="feed-item">
    <h4>
      <%= link_to version.item.name, version.item %> 
      <small>
      <%= version.event + "d" %> by <%= link_to User.find(version.whodunnit).username, User.find(version.whodunnit) %>
      </small>
    </h4>

    <ul class="list-unstyled">
      <% version.changeset.each do |data| %>
        <li>
          <strong><%= data[0].capitalize %>:</strong> 
          <% if data[1][0].present? %><p class="red">- <%= data[1][0] %></p><% end %> 
           <p class="green">+ <%= data[1][1] %></p>
        </li>
      <% end %>
    </ul>
  </div>
<% end %>

show.html.erb

<%= render :partial => '/versions/version', :object => @versions %>

events controller

  def show
    @versions = @event.versions
  end

events model

has_paper_trail

Any idea how i could put my code in a partial instead of having it inside my show view? Thanks.

EDIT: I still get a No Method Error

undefined method `item' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_PaperTrail_Version:0x007f43688ee1a0>

events_controller.rb

before_action :set_event, only: [:show, :update, :destroy]

def set_event
  @event = Event.find(params[:id])
end

Do you have defined the @event variable in the eventscontroller? The @event.versions method has to be called on an actual event object.

For example

def show
  @event = Event.find(1)
  @versions = @event.versions
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM