Skip to content
This repository was archived by the owner on Jul 25, 2025. It is now read-only.

Commit ec3535d

Browse files
Shpigfordclaude
andcommitted
Fix error pages to handle non-HTML format requests
- Add respond_to blocks in ErrorsController to handle different formats - Return appropriate HTTP status codes for non-HTML requests (JSON, XML, etc.) - Update ApplicationController#not_found to handle format types - Prevents ActionView::MissingTemplate errors for text/json requests 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 832e1a7 commit ec3535d

2 files changed

Lines changed: 37 additions & 16 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ class ApplicationController < ActionController::Base
1717
end
1818

1919
def not_found
20-
if Rails.env.production? || Rails.env.development?
21-
render "errors/not_found", status: :not_found
22-
else
23-
render file: Rails.root.join("public", "404.html"), status: :not_found, layout: false
20+
respond_to do |format|
21+
format.html do
22+
if Rails.env.production? || Rails.env.development?
23+
render "errors/not_found", status: :not_found
24+
else
25+
render file: Rails.root.join("public", "404.html"), status: :not_found, layout: false
26+
end
27+
end
28+
format.any { head :not_found }
2429
end
2530
end
2631

app/controllers/errors_controller.rb

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,43 @@ def show
55
exception = request.env["action_dispatch.exception"]
66
status_code = ActionDispatch::ExceptionWrapper.new(request.env, exception).status_code
77

8-
case status_code
9-
when 404
10-
render "not_found", status: :not_found
11-
when 422
12-
render "unprocessable_entity", status: :unprocessable_entity
13-
when 500
14-
render "internal_server_error", status: :internal_server_error
15-
else
16-
render "internal_server_error", status: :internal_server_error
8+
respond_to do |format|
9+
format.html do
10+
case status_code
11+
when 404
12+
render "not_found", status: :not_found
13+
when 422
14+
render "unprocessable_entity", status: :unprocessable_entity
15+
when 500
16+
render "internal_server_error", status: :internal_server_error
17+
else
18+
render "internal_server_error", status: :internal_server_error
19+
end
20+
end
21+
format.any do
22+
head status_code
23+
end
1724
end
1825
end
1926

2027
def not_found
21-
render status: :not_found
28+
respond_to do |format|
29+
format.html { render status: :not_found }
30+
format.any { head :not_found }
31+
end
2232
end
2333

2434
def internal_server_error
25-
render status: :internal_server_error
35+
respond_to do |format|
36+
format.html { render status: :internal_server_error }
37+
format.any { head :internal_server_error }
38+
end
2639
end
2740

2841
def unprocessable_entity
29-
render status: :unprocessable_entity
42+
respond_to do |format|
43+
format.html { render status: :unprocessable_entity }
44+
format.any { head :unprocessable_entity }
45+
end
3046
end
3147
end

0 commit comments

Comments
 (0)