Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroying UpVotes on acts_as_votable

I have a rails app that is working fine using acts_as_votable. The like button upvotes the post count, and then switches to an un-like button and this down votes the post count.

My issue is, that since I started using the Public Activity gem, I can't find a way to remove likes from the feed. I have used the following loop in the activities index view:

<% @activities.each do |activity| %>
    <p>
        <% if activity.trackable %>
            <%= link_to activity.owner.name, activity.owner %>
            <%= render_activity activity %>
        <% end %>
    </p>
<% end %>

When I delete a comment, the entire line in the activity feed of 'FOO added a comment on BAR' disappears. However, because the acts as votable gem actually creates a downvote rather than destroying the upvote, the line 'FOO liked BAR' still appears and would be subsequently followed by 'FOO unliked BAR'.

Does anybody know how I can locate the upvote by the current_user on a particular post and then destroy it?

Below is my controller code for like and unlike as it stands:

def like
  @ink.create_activity :like, owner: current_user
  @ink.upvote_by current_user

  redirect_to :back
end

def unlike
  @ink.downvote_by current_user

  redirect_to :back
end

Thanks

like image 400
abbott567 Avatar asked Feb 11 '23 17:02

abbott567


1 Answers

I know this have been answered but the ideal and easiest answer would be using the official gem method which is unvote_by it works for both upvotes and downvotes.

like image 70
Malek Zalfana Avatar answered Feb 19 '23 17:02

Malek Zalfana