votes: fix bug with removing votes multiple times.

Fix an issue where if you tried to remove the same vote multiple times,
nothing would happen.

Specifically, if you upvoted a post in one tab, then opened it in a
second tab, then removed your vote in the first tab, then upvoted it
again in the first tab, then tried to remove your vote again in the
second tab, nothing would happen when removing it in the second tab.

This was because we were removing votes by vote ID, which meant that if
a vote had already been removed, removing it again would do nothing
since it was already deleted.
This commit is contained in:
evazion
2021-11-25 23:40:44 -06:00
parent 8e32f656fa
commit 9591fa1302
2 changed files with 3 additions and 3 deletions

View File

@@ -2,7 +2,7 @@
<% if current_user.is_anonymous? %>
<%= link_to upvote_icon, login_path(url: request.fullpath), class: "post-upvote-link inactive-link" %>
<% elsif upvoted? %>
<%= link_to upvote_icon, post_vote_path(current_vote), class: "post-upvote-link post-unvote-link active-link", method: :delete, remote: true %>
<%= link_to upvote_icon, post_post_votes_path(post), class: "post-upvote-link post-unvote-link active-link", method: :delete, remote: true %>
<% else %>
<%= link_to upvote_icon, post_post_votes_path(post_id: post.id, score: 1), class: "post-upvote-link inactive-link", method: :post, remote: true %>
<% end %>
@@ -14,7 +14,7 @@
<% if current_user.is_anonymous? %>
<%= link_to downvote_icon, login_path(url: request.fullpath), class: "post-downvote-link inactive-link" %>
<% elsif downvoted? %>
<%= link_to downvote_icon, post_vote_path(current_vote), class: "post-downvote-link post-unvote-link active-link", method: :delete, remote: true %>
<%= link_to downvote_icon, post_post_votes_path(post), class: "post-downvote-link post-unvote-link active-link", method: :delete, remote: true %>
<% else %>
<%= link_to downvote_icon, post_post_votes_path(post_id: post.id, score: -1), class: "post-downvote-link inactive-link", method: :post, remote: true %>
<% end %>

View File

@@ -25,7 +25,7 @@ class PostVotesController < ApplicationController
def destroy
if params[:post_id].present?
@post_vote = PostVote.active.find_by(post_id: params[:post_id], user_id: CurrentUser.user)
@post_vote = PostVote.active.find_by(post_id: params[:post_id], user: CurrentUser.user)
@post = Post.find(params[:post_id])
else
@post_vote = PostVote.find(params[:id])