Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
56 changes: 56 additions & 0 deletions app/views/errors/not_acceptable.html.haml
Original file line number Diff line number Diff line change
@@ -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'}?
41 changes: 41 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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