Skip to content

Commit 69012e4

Browse files
authored
Merge pull request #2526 from codebar/fix/render-not-found-json-error
fix: handle JSON format in render_not_found
2 parents 8136ebd + 75e5f58 commit 69012e4

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)