2015-11-02 50 views
2

我一直在努力弄清楚如何制作一个活动页面,列出你的OWN个人帖子上其他用户喜欢的活动页面。就我而言,我没有任何朋友或跟随系统,所以任何注册的人都可以喜欢上一篇文章。我一直在到处找也找不到它是如何做使用公共活动跟踪和显示喜欢

目前一个例子,我在我的文章控制器定义等方法

def like 
    @post = Post.find(params[:id]) 
    @post.upvote_by current_user 
    redirect_to :back 
end 

我也有一个活动控制器:

class ActivitiesController < ApplicationController 
    def index 
    @activities = PublicActivity::Activity.where(trackable_type: "Post", trackable_id: current_user.post_ids, key: "post.like") 
    end 
end 

我的Post模型是这样的:

class Post < ActiveRecord::Base 
    belongs_to :user 
    acts_as_votable 

    include PublicActivity::Model 
    tracked only: [:create, :like], owner: Proc.new{ |controller, model| model.user } 

    default_scope -> { order('created_at DESC') } 
end 

我的活动视图templat e看起来像这样:

<% @activities.each do |activity| %> 
    <%= activity.inspect %> 
<% end %> 

截至目前,我的通知页面什么也没有显示。我如何去显示显示我的帖子从其他用户收到的所有喜好的提要。非常感谢

回答

0

当在帖子上调用upvote_by时,会创建一个ActsAsVotable::Vote记录。因此,如果您想记录投票,则必须将公共活动添加到此模型中。

class ActsAsVotable::Vote 
    include PublicActivity::Model 
    tracked only: [:create], owner: Proc.new{ |controller, model| model.user } 
end 
+0

这效果很好!谢谢 –

+0

@EddieCampain不错!不要忘记接受答案:) – Oleander