Skip to content

Commit 6e6daaf

Browse files
committed
[#27] Fixed issue with voting not redirecting when not logged in.
1 parent 052f75c commit 6e6daaf

File tree

5 files changed

+63
-52
lines changed

5 files changed

+63
-52
lines changed

app/assets/javascripts/application.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ document.addEventListener("DOMContentLoaded", function() {
55
const upvoteButtons = document.querySelectorAll('.upvote-button');
66
const downvoteButtons = document.querySelectorAll('.downvote-button');
77

8-
// Function to handle voting
98
function handleVote(votableId, votableType, path, articleId = null) {
109
let url;
1110
if (votableType === 'Article') {
@@ -21,9 +20,17 @@ document.addEventListener("DOMContentLoaded", function() {
2120
},
2221
body: JSON.stringify({})
2322
})
24-
.then(response => response.json())
23+
.then(response => {
24+
if (response.redirected) {
25+
window.location.href = response.url;
26+
} else {
27+
return response.json();
28+
}
29+
})
2530
.then(data => {
26-
document.getElementById(`score-${votableType}-${votableId}`).textContent = `${data.new_score} points`;
31+
if (data) {
32+
document.getElementById(`score-${votableType}-${votableId}`).textContent = `${data.new_score} points`;
33+
}
2734
})
2835
.catch(error => console.log('Error:', error));
2936
}
@@ -34,7 +41,7 @@ document.addEventListener("DOMContentLoaded", function() {
3441
const votableId = this.dataset.votableId;
3542
const votableType = this.dataset.votableType;
3643
const articleId = this.dataset.articleId;
37-
handleVote(votableId, votableType, 'upvote');
44+
handleVote(votableId, votableType, 'upvote', articleId);
3845
});
3946
});
4047

@@ -49,3 +56,4 @@ document.addEventListener("DOMContentLoaded", function() {
4956
});
5057
});
5158

59+

app/controllers/application_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ def logged_in?
1717
end
1818

1919
def authorized
20-
redirect_to login_path unless logged_in?
20+
unless logged_in?
21+
respond_to do |format|
22+
format.html { redirect_to login_path, alert: "You must be logged in to perform that action." }
23+
format.json { render json: { error: "Unauthorized" }, status: :unauthorized }
24+
end
25+
end
2126
end
2227

2328
def home?

app/controllers/articles_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class ArticlesController < ApplicationController
2-
before_action :authorized, only: [:new, :create]
2+
before_action :authorized, only: [:new, :create, :upvote, :downvote]
33

44
def index
55
@articles = Article.order(created_at: :desc).page(params[:page]).per(20)

app/views/articles/index.html.erb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<!-- app/views/articles/index.html.erb -->
2-
31
<tr style="height:10px"></tr>
42
<tr>
53
<td>
Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11
<!DOCTYPE html>
22
<html>
33
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
4-
54
<head>
65
<title>Haxxor News</title>
76
<%= csrf_meta_tags %>
8-
<%= csp_meta_tag %>
7+
<%= csp_meta_tag %>
98

10-
<%= stylesheet_link_tag 'application', media: 'all' %>
11-
<%= javascript_include_tag 'application' %>
12-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
9+
<%= stylesheet_link_tag 'application', media: 'all' %>
10+
<%= javascript_include_tag 'application' %>
11+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
1312
</head>
14-
<body>
15-
<center>
16-
<table border="0" cellpadding="0" cellspacing="0" width="85%" style="padding:2px" bgcolor="#f6f6ef">
17-
<tbody>
18-
<tr>
19-
<td bgcolor="#ff6600">
20-
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="padding:2px">
21-
<tbody>
22-
<tr>
23-
<td style="line-height:12pt; height:10px;">
24-
<span class="pagetop">
25-
<b class="hnname">
26-
<%= link_to 'Haxxor News', root_path %>
27-
</b>
28-
<%= link_to 'new', new_article_path %>
29-
</td>
30-
</span>
31-
<td style="text-align:right; padding-right:4px;">
32-
<div style="display: flex; justify-content: flex-end; gap: 5px;">
13+
<body>
14+
<center>
15+
<table border="0" cellpadding="0" cellspacing="0" width="85%" style="padding:2px" bgcolor="#f6f6ef">
16+
<tbody>
17+
<tr>
18+
<td bgcolor="#ff6600">
19+
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="padding:2px">
20+
<tbody>
21+
<tr>
22+
<td style="line-height:12pt; height:10px;">
3323
<span class="pagetop">
34-
<% if logged_in? %>
35-
<%= link_to current_user.name, user_path(current_user), class: "user-link" %>
36-
|
37-
<%= link_to "logout", logout_path, method: :delete, class: "logout-link" %>
38-
<% else %>
39-
<%= link_to "login", login_path, method: :get %>
40-
|
41-
<%= link_to "signup", signup_path, method: :get %>
42-
<% end %>
24+
<b class="hnname">
25+
<%= link_to 'Haxxor News', root_path %>
26+
</b>
27+
<%= link_to 'new', new_article_path %>
4328
</span>
44-
</div>
45-
</td>
46-
</tr>
47-
</tbody>
48-
</table>
49-
</td>
50-
</tr>
51-
<%=yield%>
29+
</td>
30+
<td style="text-align:right; padding-right:4px;">
31+
<div style="display: flex; justify-content: flex-end; gap: 5px;">
32+
<span class="pagetop">
33+
<% if logged_in? %>
34+
<%= link_to current_user.name, user_path(current_user), class: "user-link" %>
35+
|
36+
<%= link_to "logout", logout_path, method: :delete, class: "logout-link" %>
37+
<% else %>
38+
<%= link_to "login", login_path, method: :get %>
39+
|
40+
<%= link_to "signup", signup_path, method: :get %>
41+
<% end %>
42+
</span>
43+
</div>
44+
</td>
45+
</tr>
46+
</tbody>
47+
</table>
48+
</td>
49+
</tr>
50+
<%= yield %>
5251
</tbody>
53-
</table>
54-
</center>
55-
</body>
52+
</table>
53+
</center>
54+
</body>
5655
</html>
56+

0 commit comments

Comments
 (0)