Return error when unlike failed

This commit is contained in:
Steffen van Bergerem 2016-11-27 17:26:25 +01:00 committed by Benjamin Neff
parent de789267fc
commit b8d3323de0
3 changed files with 11 additions and 6 deletions

View file

@ -31,7 +31,12 @@ class LikesController < ApplicationController
end end
def destroy def destroy
@like = Like.find_by_id_and_author_id!(params[:id], current_user.person.id) begin
@like = Like.find_by_id_and_author_id!(params[:id], current_user.person.id)
rescue ActiveRecord::RecordNotFound
render text: I18n.t("likes.destroy.error"), status: 404
return
end
current_user.retract(@like) current_user.retract(@like)
respond_to do |format| respond_to do |format|

View file

@ -591,6 +591,8 @@ en:
likes: likes:
create: create:
error: "Failed to like." error: "Failed to like."
destroy:
error: "Failed to unlike."
notifications: notifications:
started_sharing: started_sharing:

View file

@ -133,14 +133,12 @@ describe LikesController, :type => :controller do
it 'does not let a user destroy other likes' do it 'does not let a user destroy other likes' do
like2 = eve.like!(@message) like2 = eve.like!(@message)
like_count = Like.count like_count = Like.count
expect {
delete :destroy, :format => :json, id_field => like2.target_id, :id => like2.id
}.to raise_error(ActiveRecord::RecordNotFound)
delete :destroy, :format => :json, id_field => like2.target_id, :id => like2.id
expect(response.status).to eq(404)
expect(response.body).to eq(I18n.t("likes.destroy.error"))
expect(Like.count).to eq(like_count) expect(Like.count).to eq(like_count)
end end
end end
end end