diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f1b7f6656..93248a73e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,6 +18,7 @@ class ApplicationController < ActionController::Base rescue_from ActionController::RoutingError, with: :render_not_found rescue_from ActiveRecord::RecordNotFound, with: :render_not_found + rescue_from ActionController::UnknownFormat, with: :render_not_acceptable rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized rescue_from Pundit::AuthorizationNotPerformedError, with: :user_not_authorized @@ -36,6 +37,13 @@ def render_not_found end end + def render_not_acceptable + respond_to do |format| + format.html { render template: 'errors/not_acceptable', layout: false, status: :not_acceptable } + format.all { head :not_acceptable } + end + end + protected def current_user diff --git a/app/views/errors/not_acceptable.html.haml b/app/views/errors/not_acceptable.html.haml new file mode 100644 index 000000000..897525170 --- /dev/null +++ b/app/views/errors/not_acceptable.html.haml @@ -0,0 +1,56 @@ +!!! +%html + %head + = render partial: 'shared/meta_tags' + = favicon_link_tag 'favicon.ico' + %title codebar.io - Not acceptable + :css + body { + font-family: Helvetica, arial, sans-serif; + font-size: 14px; + line-height: 1.6; + padding-top: 10px; + padding-bottom: 10px; + background-color: white; + padding: 30px; + } + h1 { + color: #652f93; + font-size: 2em; + } + a { + color: #652f93; + } + a:visited, + a:link, + a:active { + text-decoration: none; + } + a:hover { + text-decoration: underline; + } + h1,h2,h3,h4,h5,h6 { + margin: 20px 0 10px; + padding: 0; + font-weight: bold; + -webkit-font-smoothing: antialiased; + cursor: text; + position: relative; + } + + #main { + text-align: center; + width: 80%; + min-width: 680px; + margin: 0 auto; + } + + %body + #main + =link_to root_path do + =image_tag 'codebar-girl.gif', height: '200px', id: 'animated-logo' + #message + %h1 Not acceptable + %h2 The requested format is not supported. + %h3 + In the meantime why don't you #{link_to 'join our community on Slack', 'https://slack.codebar.io', target: '_blank'}? diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 2f8e24981..c1f28d21a 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -28,4 +28,45 @@ def index end end end + + describe '#render_not_acceptable' do + controller do + def index + raise ActionController::UnknownFormat + end + end + + context 'with HTML format' do + before do + get :index, format: :html + end + + it 'renders the not_acceptable template' do + expect(response.status).to eq(406) + expect(response).not_to be_redirect + end + end + + context 'with JSON format' do + before do + get :index, format: :json + end + + it 'returns empty 406 response' do + expect(response.status).to eq(406) + expect(response.body).to be_empty + end + end + + context 'with XML format' do + before do + get :index, format: :xml + end + + it 'returns empty 406 response' do + expect(response.status).to eq(406) + expect(response.body).to be_empty + end + end + end end