From 9591fa1302aaf3799ba3960832569d8d7a38cf43 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 25 Nov 2021 23:40:44 -0600 Subject: [PATCH] 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. --- .../post_votes_component/post_votes_component.html.erb | 4 ++-- app/controllers/post_votes_controller.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/components/post_votes_component/post_votes_component.html.erb b/app/components/post_votes_component/post_votes_component.html.erb index f099b162a..89b84247d 100644 --- a/app/components/post_votes_component/post_votes_component.html.erb +++ b/app/components/post_votes_component/post_votes_component.html.erb @@ -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 %> diff --git a/app/controllers/post_votes_controller.rb b/app/controllers/post_votes_controller.rb index 7a6232b41..a1cd76f1d 100644 --- a/app/controllers/post_votes_controller.rb +++ b/app/controllers/post_votes_controller.rb @@ -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])