post votes: fix error handling.
* Clean up javascript.
* Return HTTP 422 instead of HTTP 500 on "you have already voted for
this post" errors.
* In json/xml error responses, return the error message in the `message`
field, not `reason`.
* In json/xml success responses, return the post itself instead of a
plain `{ success: true }` object.
This commit is contained in:
@@ -1,20 +1,20 @@
|
|||||||
class PostVotesController < ApplicationController
|
class PostVotesController < ApplicationController
|
||||||
before_action :voter_only
|
before_action :voter_only
|
||||||
skip_before_action :api_check
|
skip_before_action :api_check
|
||||||
|
respond_to :js, :json, :xml
|
||||||
|
rescue_with PostVote::Error, status: 422
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@post = Post.find(params[:post_id])
|
@post = Post.find(params[:post_id])
|
||||||
@post.vote!(params[:score])
|
@post.vote!(params[:score])
|
||||||
rescue PostVote::Error, ActiveRecord::RecordInvalid => x
|
|
||||||
@error = x
|
respond_with(@post)
|
||||||
render status: 500
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@post = Post.find(params[:post_id])
|
@post = Post.find(params[:post_id])
|
||||||
@post.unvote!
|
@post.unvote!
|
||||||
rescue PostVote::Error => x
|
|
||||||
@error = x
|
respond_with(@post)
|
||||||
render status: 500
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
<% if @error %>
|
Danbooru.Utility.notice("Vote saved");
|
||||||
$(window).trigger("danbooru:notice", "<%= j @error.to_s %>");
|
$("#score-for-post-<%= @post.id %>").html(<%= @post.score %>);
|
||||||
<% else %>
|
|
||||||
$(window).trigger("danbooru:notice", "Vote saved");
|
|
||||||
$("#score-for-post-<%= @post.id %>").html(<%= @post.score %>);
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
$("#vote-links-for-post-<%= @post.id %>").hide();
|
$("#vote-links-for-post-<%= @post.id %>").hide();
|
||||||
$("#unvote-link-for-post-<%= @post.id %>").show();
|
$("#unvote-link-for-post-<%= @post.id %>").show();
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
<% if @error %>
|
|
||||||
{"success": false, "reason": <%= @error.to_s.to_json.html_safe %>}
|
|
||||||
<% else %>
|
|
||||||
{"success": true}
|
|
||||||
<% end %>
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<% if @error %>
|
|
||||||
<response success="false"><%= @error.to_s %></response>
|
|
||||||
<% else %>
|
|
||||||
<response success="true"/>
|
|
||||||
<% end %>
|
|
||||||
@@ -1,8 +1,5 @@
|
|||||||
<% if @error %>
|
Danbooru.Utility.notice("Unvoted successfully");
|
||||||
$(window).trigger("danbooru:notice", "<%= j @error.to_s %>");
|
$("#score-for-post-<%= @post.id %>").html(<%= @post.score %>);
|
||||||
<% else %>
|
|
||||||
$(window).trigger("danbooru:notice", "Unvoted successfully");
|
|
||||||
$("#score-for-post-<%= @post.id %>").html(<%= @post.score %>);
|
|
||||||
<% end %>
|
|
||||||
$("#vote-links-for-post-<%= @post.id %>").show();
|
$("#vote-links-for-post-<%= @post.id %>").show();
|
||||||
$("#unvote-link-for-post-<%= @post.id %>").hide();
|
$("#unvote-link-for-post-<%= @post.id %>").hide();
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
<% if @error %>
|
|
||||||
{"success": false, "reason": <%= @error.to_s.to_json.html_safe %>}
|
|
||||||
<% else %>
|
|
||||||
{"success": true}
|
|
||||||
<% end %>
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<% if @error %>
|
|
||||||
<response success="false"><%= @error.to_s %></response>
|
|
||||||
<% else %>
|
|
||||||
<response success="true"/>
|
|
||||||
<% end %>
|
|
||||||
Reference in New Issue
Block a user