From 25776a062c3e20da67d4418c15e4ee9f2c59ac13 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 31 Mar 2020 19:12:57 -0500 Subject: [PATCH] posts/random: fix pundit exception when no post is found. --- app/controllers/posts_controller.rb | 3 ++- test/functional/posts_controller_test.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index a469130a8..738caa180 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -80,8 +80,9 @@ class PostsController < ApplicationController end def random - @post = authorize Post.tag_match(params[:tags]).random + @post = Post.tag_match(params[:tags]).random raise ActiveRecord::RecordNotFound if @post.nil? + authorize @post respond_with(@post) do |format| format.html { redirect_to post_path(@post, :tags => params[:tags]) } end diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index 83bd9967e..c4ac38dcd 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -205,6 +205,11 @@ class PostsControllerTest < ActionDispatch::IntegrationTest get random_posts_path, params: { tags: "aaaa" } assert_redirected_to(post_path(@post, tags: "aaaa")) end + + should "return a 404 when no random posts can be found" do + get random_posts_path, params: { tags: "qoigjegoi" } + assert_response 404 + end end context "show action" do