Skip to content

Commit 07a3950

Browse files
committed
render layouts properly
1 parent 2ad4bf0 commit 07a3950

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<%= yield %>
4+
</body>
5+
</html>

app/views/restful_error/show.json.erb

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
raw JSON.generate(
2+
status_code: @status_code,
3+
reason_phrase: @reason_phrase,
4+
response_message: @response_message
5+
)

lib/restful_error/exceptions_app.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ module RestfulError
55
class ExceptionsController < ::ActionController::Metal
66
include AbstractController::Rendering
77
include ActionView::Layouts
8+
include ActionController::Rendering
89

10+
def self.controller_path = "restful_error"
911
append_view_path File.join(File.dirname(__FILE__), "../../app/views")
12+
layout nil
1013

1114
def show
1215
@exception = request.env["action_dispatch.exception"]
@@ -16,8 +19,7 @@ def show
1619
@reason_phrase = status.reason_phrase
1720
@response_message = @exception.try(:response_message) || RestfulError.localized_phrase(@exception.class.name, status) || nil
1821

19-
self.status = status.code
20-
render "restful_error/show", formats: request.format.symbol
22+
render status: status.code, formats: request.format.symbol
2123
end
2224
end
2325

spec/with_rails/exceptions_app_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def app = RestfulError::ExceptionsApp
1010

1111
shared_context "html" do
12-
let(:request) { get "/#{status_code}" }
12+
let(:request) { get "/#{status_code}", {}, 'HTTP_ACCEPT' => 'text/html' }
1313
let(:body) { request; last_response.body }
1414
end
1515
shared_context "json" do
@@ -22,16 +22,19 @@ def app = RestfulError::ExceptionsApp
2222
end
2323
describe RestfulError[404] do
2424
let(:status_code) { 404 }
25-
include_context "html" do
25+
context 'html' do
26+
include_context "html"
2627
context 'default message' do
2728
let(:exception) { described_class.new }
2829
it do
29-
expect(body).to include "Page not found"
30+
expect(body).to include "<p>Page not found</p>"
31+
expect(body).to include "</html>" # layout is rendered
3032
expect(last_response.status).to eq status_code
3133
end
3234
end
3335
end
34-
include_context "json" do
36+
context 'json' do
37+
include_context "json"
3538
context 'default message' do
3639
let(:exception) { described_class.new }
3740
it do

0 commit comments

Comments
 (0)