我正在使用 Public_Activity gem 来显示网站上正在执行的操作列表。我已经在主页上有一个主要提要,但我还想包括使用该 gem 跟踪的所有活动。
这是我在控制器内部尝试做的事情
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build
@feed_activities = PublicActivity::Activity.order("created_at desc")
@feed_posts = current_user.feed.without_review.paginate(page: params[:page])
@feed_items = @feed_posts + @feed_activities
end
end
然后在我看来,这就是我试图称呼它的方式
<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
上面调用的 _feed_item.html 是
<li id="<%= feed_item.id %>">
<br>
<% if @feed_activities? %>
<%= link_to activity.owner.name, activity.owner if activity.owner %> has posted
<% else %>
<div class ="gravatarhome"><%= link_to gravatar_for(feed_item.user), feed_item.user %></div>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="textname">shared this</span><span class="timestamp"> <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<div class="FeedContent"><%= truncate(feed_item.content, :length=>150, :omission=>' ...(continued)') %>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
confirm: "You sure?",
title: feed_item.content %>
<% end %><br>
<% end %>
</li>
但是,我收到此错误
NameError in Static_pages#home
Showing C:/app/views/shared/_feed_item.html.erb where line #5 raised:
undefined local variable or method `activity' for #<#<Class:0x6ef3b98>:0x60d50d8>
任何人都知道是否有更好的方法来做到这一点,以及需要在哪里定义活动变量才能使其工作?
更新:这是我使用 gem 的 application_controller
class ApplicationController < ActionController::Base
include PublicActivity::StoreController