Skip to content

Commit

Permalink
Any unmatched route render 404 file directly
Browse files Browse the repository at this point in the history
We've had some errors reported by Render since spammy requests coming in (e.g. POSTing to non-existent routes) so as a last resort in the routes file, any unmatched routes, just render the default 404 error page directly to avoid raising any errors.
  • Loading branch information
javierjulio committed Aug 16, 2024
1 parent a918895 commit d219361
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

def route_not_found
render file: Rails.public_path.join("404.html"), status: :not_found, layout: false
end
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check
get "up", to: "rails/health#show", as: :rails_health_check

root to: redirect("admin")

match "*unmatched", to: "application#route_not_found", via: :all
end
9 changes: 9 additions & 0 deletions test/system/active_admin/route_not_found_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "application_system_test_case"

class RouteNotFoundTest < ApplicationSystemTestCase
test "visiting non-existent route renders 404 page" do
visit "/does-not-exist"

assert_text "The page you were looking for doesn't exist."
end
end

0 comments on commit d219361

Please sign in to comment.