我正在尝试将一些代码放在/ events中的show页面中。当我在我的显示页面中使用它时,代码工作正常,但是当我将其提取为部分时,我得到的是无方法错误
undefined method `event' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_PaperTrail_Version:0x007f4368823a68>
版本/ _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 %>
事件控制器
def show
@versions = @event.versions
end
事件模型
has_paper_trail
我知道如何将我的代码放入局部而不是将其放入我的展示视图中吗?感谢。
修改 我仍然得到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
答案 0 :(得分:0)
您是否在eventscontroller中定义了@event
变量?
必须在实际的事件对象上调用@event.versions
方法。
例如
def show
@event = Event.find(1)
@versions = @event.versions
end