diff --git a/app/controllers/comment_votes_controller.rb b/app/controllers/comment_votes_controller.rb index 835412677..1cd22afd1 100644 --- a/app/controllers/comment_votes_controller.rb +++ b/app/controllers/comment_votes_controller.rb @@ -8,7 +8,7 @@ class CommentVotesController < ApplicationController @comment_vote = @comment.vote!(params[:score]) rescue CommentVote::Error, ActiveRecord::RecordInvalid => x @error = x - render status: 500 + render status: 422 end def destroy @@ -16,6 +16,6 @@ class CommentVotesController < ApplicationController @comment.unvote! rescue CommentVote::Error => x @error = x - render status: 500 + render status: 422 end end diff --git a/app/views/comment_votes/create.json.erb b/app/views/comment_votes/create.json.erb index 4b0d6500f..313a907c0 100644 --- a/app/views/comment_votes/create.json.erb +++ b/app/views/comment_votes/create.json.erb @@ -1,4 +1,6 @@ -<% if @comment_vote.errors.any? %> +<% if @error %> + {"success": false, "errors": <%= @error.to_s.to_json.html_safe %>} +<% elsif @comment_vote.errors.any? %> {"success": false, "errors": <%= @comment_vote.errors.full_messages.to_json.html_safe %>} <% else %> {"success": true} diff --git a/test/functional/comment_votes_controller_test.rb b/test/functional/comment_votes_controller_test.rb index 953b500aa..740037b0a 100644 --- a/test/functional/comment_votes_controller_test.rb +++ b/test/functional/comment_votes_controller_test.rb @@ -28,7 +28,7 @@ class CommentVotesControllerTest < ActionController::TestCase assert_difference("CommentVote.count", 0) do post :create, {:format => "json", :comment_id => @comment.id, :score => -1}, {:user_id => @user.id} assert_response 422 - assert_equal("{\"errors\":{\"user_id\":[\"have already voted for this comment\"]}}", @response.body.strip) + assert_equal("{\"success\": false, \"errors\": \"Validation failed: You have already voted for this comment\"}", @response.body.strip) end end end @@ -41,11 +41,11 @@ class CommentVotesControllerTest < ActionController::TestCase end end - should "fail silently on errors" do + should "fail on errors" do FactoryGirl.create(:comment_vote, :comment => @comment, :score => -1) assert_difference("CommentVote.count", 0) do post :create, {:format => "js", :comment_id => @comment.id, :score => -1}, {:user_id => @user.id} - assert_response :success + assert_response 422 end end end