0

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 %>
Sawo Cliff
  • 2,888
  • 1
  • 17
  • 21

1 Answers1

1

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 %>
spickermann
  • 100,941
  • 9
  • 101
  • 131
  • This did it. I am also having another issue with public activity and tracking posts commented on. I have this in my create.html.erb for comment <%= link_to activity.trackable.post.body, activity.trackable.post %> the error i'm getting is undefined method `post' for nil:NilClass. Comment controller create action has if @comment.save @comment.create_activity :create, owner: current_user . Where might I be going awry with this – Sawo Cliff Nov 15 '17 at 12:04
  • 1
    That sounds like a different, unrelated problem. Let's wrap up this question by accepting the answer if the error in your question is gone. And ask a new question and paste all relevant source code. Discussing further details here in the comments is very hard and doesn't make much sense. – spickermann Nov 15 '17 at 15:05