diff --git a/app/controllers/wiki_pages_controller.rb b/app/controllers/wiki_pages_controller.rb index 41b7a2b59..a8d97356f 100644 --- a/app/controllers/wiki_pages_controller.rb +++ b/app/controllers/wiki_pages_controller.rb @@ -40,17 +40,19 @@ class WikiPagesController < ApplicationController end def show - if params[:id] =~ /\A\d+\Z/ + if params[:id] =~ /\A\d+\z/ @wiki_page = WikiPage.find(params[:id]) else - @wiki_page = WikiPage.find_by_title(params[:id]) - if @wiki_page.nil? && request.format.symbol == :html - redirect_to show_or_new_wiki_pages_path(:title => params[:id]) - return - end + @wiki_page = WikiPage.titled(params[:id]).first + end + + if @wiki_page.present? + respond_with(@wiki_page) + elsif request.format.html? + redirect_to show_or_new_wiki_pages_path(title: params[:id]) + else + raise ActiveRecord::RecordNotFound end - - respond_with(@wiki_page) end def create diff --git a/test/functional/wiki_pages_controller_test.rb b/test/functional/wiki_pages_controller_test.rb index 45e950d03..7304f47e4 100644 --- a/test/functional/wiki_pages_controller_test.rb +++ b/test/functional/wiki_pages_controller_test.rb @@ -48,11 +48,16 @@ class WikiPagesControllerTest < ActionDispatch::IntegrationTest assert_response :success end - should "redirect for a nonexistent title" do - get wiki_page_path(:id => "what") + should "redirect html requests for a nonexistent title" do + get wiki_page_path("what") assert_redirected_to(show_or_new_wiki_pages_path(title: "what")) end + should "return 404 to api requests for a nonexistent title" do + get wiki_page_path("what"), as: :json + assert_response 404 + end + should "render for a negated tag" do as_user do @wiki_page.update(title: "-aaa")