Skip to content
Open in github.dev Open in a new github.dev tab Open in codespace

make comment upvoting work without js - #2073

Merged
pushcx merged 2 commits into
lobsters:mainfrom
adamdruppe:nojs_comment_vote
Jul 9, 2026
Lines changed: 26 additions & 5 deletions

Pull request toolbar

Merged
make comment upvoting work without js#2073
0 of 5 files viewed

‎app/assets/stylesheets/application.css‎

Lines changed: 1 addition & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -519,6 +519,7 @@
519
align-items: center;
519
align-items: center;
520
justify-self: center;
520
justify-self: center;
521
text-decoration: none;
521
text-decoration: none;
522+
border: none;
522
}
523
}
523
.upvoter:focus { outline: none; } /* don't outline after upvote click */
524
.upvoter:focus { outline: none; } /* don't outline after upvote click */
524
.upvoter:focus-visible { outline: 2px solid var(--color-fg-link); } /* do outline when tabbing */
525
.upvoter:focus-visible { outline: 2px solid var(--color-fg-link); } /* do outline when tabbing */

‎app/controllers/comments_controller.rb‎

Lines changed: 10 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -239,7 +239,11 @@ def unvote
239
0, comment.story_id, comment.id, @user.id, nil
239
0, comment.story_id, comment.id, @user.id, nil
240
)
240
)
241

241

242-
render plain: "ok"
242+
if request.xhr?
243+
render plain: "ok"
244+
else
245+
redirect_to(Routes.comment_target_path(comment, true))
246+
end
243
end
247
end
244

248

245
def upvote
249
def upvote
@@ -251,7 +255,11 @@ def upvote
251
1, comment.story_id, comment.id, @user.id, nil
255
1, comment.story_id, comment.id, @user.id, nil
252
)
256
)
253

257

254-
render plain: "ok"
258+
if request.xhr?
259+
render plain: "ok"
260+
else
261+
redirect_to(Routes.comment_target_path(comment, true))
262+
end
255
end
263
end
256

264

257
def flag
265
def flag

‎app/javascript/application.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -907,7 +907,7 @@ document.addEventListener("DOMContentLoaded", () => {
907
Lobster.removeFlagModal()
907
Lobster.removeFlagModal()
908
});
908
});
909

909

910-
on("click", '.comment a.upvoter', (event) => {
910+
on("click", '.comment button.upvoter', (event) => {
911
event.preventDefault();
911
event.preventDefault();
912
Lobster.upvoteComment(event.target);
912
Lobster.upvoteComment(event.target);
913
});
913
});

‎app/views/comments/_comment.html.erb‎

Lines changed: 7 additions & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -30,7 +30,13 @@
30
<% end %>
30
<% end %>
31
<% score = comment_score_for_user(comment, @user)%>
31
<% score = comment_score_for_user(comment, @user)%>
32
<% if !comment.is_gone? and !comment.previewing %>
32
<% if !comment.is_gone? and !comment.previewing %>
33-
<%= link_to upvoter_score(score), (@user ? '': login_path), :class => "upvoter", :title => score %>
33+
<% if @user %>
34+
<%= form_tag("/comments/#{comment.short_id}/#{comment.current_upvoted? ? "unvote" : "upvote"}") do %>
35+
<button type="submit" class="upvoter" title="<%= upvoter_score(score) %>"><%= upvoter_score(score) %></button>
36+
<% end %>
37+
<% else %>
38+
<%= link_to upvoter_score(score), login_path, :class => "upvoter", :title => score %>
39+
<% end %>
34
<% elsif @user&.is_moderator? %>
40
<% elsif @user&.is_moderator? %>
35
<span class="upvoter"><%= score %></span>
41
<span class="upvoter"><%= score %></span>
36
<% end %>
42
<% end %>

‎app/views/comments/_threads.html.erb‎

Lines changed: 7 additions & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -74,7 +74,13 @@
74
<% end %>
74
<% end %>
75
<% score = comment_score_for_user(comment, @user)%>
75
<% score = comment_score_for_user(comment, @user)%>
76
<% if !comment.is_gone? and !comment.previewing %>
76
<% if !comment.is_gone? and !comment.previewing %>
77-
<%= link_to upvoter_score(score), (@user ? '': login_path), :class => "upvoter", :title => score %>
77+
<% if @user %>
78+
<%= form_tag("/comments/#{comment.short_id}/#{comment.current_upvoted? ? "unvote" : "upvote"}") do %>
79+
<button type="submit" class="upvoter" title="<%= upvoter_score(score) %>"><%= upvoter_score(score) %></button>
80+
<% end %>
81+
<% else %>
82+
<%= link_to upvoter_score(score), login_path, :class => "upvoter", :title => score %>
83+
<% end %>
78
<% elsif @user&.is_moderator? %>
84
<% elsif @user&.is_moderator? %>
79
<span class="upvoter"><%= score %></span>
85
<span class="upvoter"><%= score %></span>
80
<% end %>
86
<% end %>