我使用公共活动为关注的人的活动提供通知。我想显示用户是否以以下格式关注另一个用户
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 %>