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:
@@ -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'
|
||||
|
||||
15
spec/controllers/related_contents_controller_spec.rb
Normal file
15
spec/controllers/related_contents_controller_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user