diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e11faaab7..f1b7f6656 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -30,7 +30,10 @@ class ApplicationController < ActionController::Base before_action :accept_terms, if: :logged_in? def render_not_found - render template: 'errors/not_found', layout: false, status: :not_found + respond_to do |format| + format.html { render template: 'errors/not_found', layout: false, status: :not_found } + format.all { head :not_found } + end end protected diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb new file mode 100644 index 000000000..2f8e24981 --- /dev/null +++ b/spec/controllers/application_controller_spec.rb @@ -0,0 +1,31 @@ +RSpec.describe ApplicationController do + describe '#render_not_found' do + controller do + def index + raise ActiveRecord::RecordNotFound + end + end + + context 'with HTML format' do + before do + get :index, format: :html + end + + it 'renders the not_found template' do + expect(response.status).to eq(404) + expect(response).not_to be_redirect + end + end + + context 'with JSON format' do + before do + get :index, format: :json + end + + it 'returns empty 404 response' do + expect(response.status).to eq(404) + expect(response.body).to be_empty + end + end + end +end