Skip to content

Commit d219361

Browse files
committed
Any unmatched route render 404 file directly
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.
1 parent a918895 commit d219361

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
class ApplicationController < ActionController::Base
2+
protect_from_forgery with: :exception
3+
4+
def route_not_found
5+
render file: Rails.public_path.join("404.html"), status: :not_found, layout: false
6+
end
27
end

config/routes.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

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

99
root to: redirect("admin")
10+
11+
match "*unmatched", to: "application#route_not_found", via: :all
1012
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "application_system_test_case"
2+
3+
class RouteNotFoundTest < ApplicationSystemTestCase
4+
test "visiting non-existent route renders 404 page" do
5+
visit "/does-not-exist"
6+
7+
assert_text "The page you were looking for doesn't exist."
8+
end
9+
end

0 commit comments

Comments
 (0)