From 9a233935358a94da2f750b5ee750f0d060985792 Mon Sep 17 00:00:00 2001 From: Julian Herrero Date: Thu, 17 Jan 2019 10:35:53 +0100 Subject: [PATCH] Use find instead of find_by_id This method will raise an exception if resource is not found when trying to call score_action on nil. Prefer to raise a 404 HTML NotFound error instead. --- app/controllers/related_contents_controller.rb | 2 +- .../related_contents_controller_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 spec/controllers/related_contents_controller_spec.rb diff --git a/app/controllers/related_contents_controller.rb b/app/controllers/related_contents_controller.rb index db6be0018..9dc753fee 100644 --- a/app/controllers/related_contents_controller.rb +++ b/app/controllers/related_contents_controller.rb @@ -31,7 +31,7 @@ class RelatedContentsController < ApplicationController private def score(action) - @related = RelatedContent.find_by(id: params[:id]) + @related = RelatedContent.find params[:id] @related.send("score_#{action}", current_user) render template: 'relationable/_refresh_score_actions' diff --git a/spec/controllers/related_contents_controller_spec.rb b/spec/controllers/related_contents_controller_spec.rb new file mode 100644 index 000000000..c792a267e --- /dev/null +++ b/spec/controllers/related_contents_controller_spec.rb @@ -0,0 +1,15 @@ +require "rails_helper" + +describe RelatedContentsController do + + describe "#score" do + it "raises an error if related content does not exist" do + controller.params[:id] = 0 + + expect do + controller.send(:score, "action") + end.to raise_error ActiveRecord::RecordNotFound + end + end + +end