Skip to content

Commit 127819d

Browse files
authored
Merge pull request #2600 from mroderick/fix/unknown-format-rescue
fix: handle ActionController::UnknownFormat with 406 response
2 parents aad176b + 5163bef commit 127819d

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ApplicationController < ActionController::Base
1818

1919
rescue_from ActionController::RoutingError, with: :render_not_found
2020
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
21+
rescue_from ActionController::UnknownFormat, with: :render_not_acceptable
2122

2223
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
2324
rescue_from Pundit::AuthorizationNotPerformedError, with: :user_not_authorized
@@ -36,6 +37,13 @@ def render_not_found
3637
end
3738
end
3839

40+
def render_not_acceptable
41+
respond_to do |format|
42+
format.html { render template: 'errors/not_acceptable', layout: false, status: :not_acceptable }
43+
format.all { head :not_acceptable }
44+
end
45+
end
46+
3947
protected
4048

4149
def current_user
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
!!!
2+
%html
3+
%head
4+
= render partial: 'shared/meta_tags'
5+
= favicon_link_tag 'favicon.ico'
6+
%title codebar.io - Not acceptable
7+
:css
8+
body {
9+
font-family: Helvetica, arial, sans-serif;
10+
font-size: 14px;
11+
line-height: 1.6;
12+
padding-top: 10px;
13+
padding-bottom: 10px;
14+
background-color: white;
15+
padding: 30px;
16+
}
17+
h1 {
18+
color: #652f93;
19+
font-size: 2em;
20+
}
21+
a {
22+
color: #652f93;
23+
}
24+
a:visited,
25+
a:link,
26+
a:active {
27+
text-decoration: none;
28+
}
29+
a:hover {
30+
text-decoration: underline;
31+
}
32+
h1,h2,h3,h4,h5,h6 {
33+
margin: 20px 0 10px;
34+
padding: 0;
35+
font-weight: bold;
36+
-webkit-font-smoothing: antialiased;
37+
cursor: text;
38+
position: relative;
39+
}
40+
41+
#main {
42+
text-align: center;
43+
width: 80%;
44+
min-width: 680px;
45+
margin: 0 auto;
46+
}
47+
48+
%body
49+
#main
50+
=link_to root_path do
51+
=image_tag 'codebar-girl.gif', height: '200px', id: 'animated-logo'
52+
#message
53+
%h1 Not acceptable
54+
%h2 The requested format is not supported.
55+
%h3
56+
In the meantime why don't you #{link_to 'join our community on Slack', 'https://slack.codebar.io', target: '_blank'}?

spec/controllers/application_controller_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,45 @@ def index
2828
end
2929
end
3030
end
31+
32+
describe '#render_not_acceptable' do
33+
controller do
34+
def index
35+
raise ActionController::UnknownFormat
36+
end
37+
end
38+
39+
context 'with HTML format' do
40+
before do
41+
get :index, format: :html
42+
end
43+
44+
it 'renders the not_acceptable template' do
45+
expect(response.status).to eq(406)
46+
expect(response).not_to be_redirect
47+
end
48+
end
49+
50+
context 'with JSON format' do
51+
before do
52+
get :index, format: :json
53+
end
54+
55+
it 'returns empty 406 response' do
56+
expect(response.status).to eq(406)
57+
expect(response.body).to be_empty
58+
end
59+
end
60+
61+
context 'with XML format' do
62+
before do
63+
get :index, format: :xml
64+
end
65+
66+
it 'returns empty 406 response' do
67+
expect(response.status).to eq(406)
68+
expect(response.body).to be_empty
69+
end
70+
end
71+
end
3172
end

0 commit comments

Comments
 (0)