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.
This commit is contained in:
Julian Herrero
2019-01-17 10:35:53 +01:00
parent 23b6e38915
commit 9a23393535
2 changed files with 16 additions and 1 deletions

View File

@@ -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'

View File

@@ -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