Im using public activity to provide notifications for activities of people followed. I want to show if a user follows another in the format of
John Doe followed Sam Smith
But all I can achieve so far is
John Doe followed 1
here is my code. Relationships controller
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
Relationship model
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
Relationship create file inside public_activity folder
<% if activity.trackable %>
Followed <%= activity.trackable.followed_id %>
<% else %>
Unfollowed a User
<% end %>
Instead of just rendering a followed_id
you might want to render the followed.name
instead. Change:
Followed <%= activity.trackable.followed_id %>
to something like this (replace name
with a method that makes sense in your application):
Followed <%= activity.trackable.followed.name %>