0

我使用公共活动为关注的人的活动提供通知。我想显示用户是否以以下格式关注另一个用户

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 %>
4

1 回答 1

1

而不是仅仅渲染 afollowed_id你可能想要渲染 the followed.name。改变:

Followed <%= activity.trackable.followed_id %>

像这样(替换name为在您的应用程序中有意义的方法):

Followed <%= activity.trackable.followed.name %>
于 2017-11-15T11:47:47.507 回答