Skip to content

Commit 75e5f58

Browse files
committed
fix: handle JSON format in render_not_found
Reverts the render_not_found method to use respond_to, matching the pattern used in the exception handler. This fixes MissingTemplate errors when requests come in with JSON format (e.g., from API clients). The previous change (3eeca62) simplified render_not_found but broke JSON responses. Now HTML requests get the proper 404 page while API requests get an empty 404 response. Adds tests for both HTML and JSON format handling.
1 parent 8136ebd commit 75e5f58

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

app/controllers/application_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ class ApplicationController < ActionController::Base
3030
before_action :accept_terms, if: :logged_in?
3131

3232
def render_not_found
33-
render template: 'errors/not_found', layout: false, status: :not_found
33+
respond_to do |format|
34+
format.html { render template: 'errors/not_found', layout: false, status: :not_found }
35+
format.all { head :not_found }
36+
end
3437
end
3538

3639
protected
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
RSpec.describe ApplicationController do
2+
describe '#render_not_found' do
3+
controller do
4+
def index
5+
raise ActiveRecord::RecordNotFound
6+
end
7+
end
8+
9+
context 'with HTML format' do
10+
before do
11+
get :index, format: :html
12+
end
13+
14+
it 'renders the not_found template' do
15+
expect(response.status).to eq(404)
16+
expect(response).not_to be_redirect
17+
end
18+
end
19+
20+
context 'with JSON format' do
21+
before do
22+
get :index, format: :json
23+
end
24+
25+
it 'returns empty 404 response' do
26+
expect(response.status).to eq(404)
27+
expect(response.body).to be_empty
28+
end
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)