使用公共活动来显示被关注的人

时间:2017-11-15 11:36:37

标签: ruby-on-rails ruby public-activity

我正在使用公共活动为所关注的人的活动提供通知。我希望显示用户是否以

的格式跟随另一个用户
John Doe followed Sam Smith

但到目前为止我所能达到的只是

John Doe followed 1

这是我的代码。关系控制器

class RelationshipsController < ApplicationController
 before_action :authenticate_user!

 def create
  @user = User.find(params[:followed_id])
  current_user.follow(@user)
  respond_to do |format|
    format.html { redirect_to @user }
    format.js
  end
 end

 def destroy
  @user = Relationship.find(params[:id]).followed
  current_user.unfollow(@user)
  respond_to do |format|
   format.html { redirect_to @user }
   format.js
  end
 end
end

关系模型

class Relationship < ApplicationRecord
 include PublicActivity::Model
  tracked owner: ->(controller, model) { controller && controller.current_user }

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

public_activity文件夹中的关系创建文件

<% if activity.trackable %>
  Followed <%= activity.trackable.followed_id %>
<% else %>
  Unfollowed a User
 <% end %>

1 个答案:

答案 0 :(得分:1)

您可能想要渲染followed_id,而不仅仅是渲染followed.name。变化:

Followed <%= activity.trackable.followed_id %>

这样的事情(用你的应用程序中有意义的方法替换name):

Followed <%= activity.trackable.followed.name %>