将两个偏导数呈现为一个(rails 3),我这样做对吗?



我正在使用Public_Activity 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>

有人知道是否有更好的方法来做到这一点,以及活动变量需要在哪里定义才能使其工作?

更新:这是我的application_controller,它使用gem

class ApplicationController < ActionController::Base
  include PublicActivity::StoreController

错误说明了问题,rails不知道指向哪个活动。

既没有在控制器中加载activity,也没有关联到feed_item或者rails不知道哪个活动指向

activity是这个部分中没有定义的变量,所以它会给出错误。如果你想通过集合循环所有的活动,那么你必须传递变量作为集合,同时呈现部分并使用部分名称调用,像这样:

<%= link_to feed_item.owner.name, feed_item.owner if feed_item.owner %> has posted

将activity替换为partial中的feed_item。它会解决你的问题。