Skip to content

Commit fe4e15b

Browse files
committed
display 404 page: invalid route / record not found
- rescue_from ActionController::RoutingError doesn’t do anything - `unless Rails.env.production?` made record not found work - added tests! :-) - new errors controller - handle 422 & 500 errors with public pages - annotate routes
1 parent 73fe695 commit fe4e15b

File tree

12 files changed

+345
-273
lines changed

12 files changed

+345
-273
lines changed

app/controllers/application_controller.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class ApplicationController < ActionController::Base
2121
after_action :record_visit
2222
after_action :record_location
2323

24-
rescue_from ActiveRecord::RecordNotFound, with: :render_404 unless Rails.env.production?
25-
rescue_from ActionController::RoutingError, with: :render_404 unless Rails.env.production?
24+
rescue_from ActiveRecord::RecordNotFound, with: :render_404
2625

2726
protected
2827

@@ -183,7 +182,7 @@ def not_on_achievements?
183182
end
184183

185184
def render_404
186-
render template: 'error/not_found', status: :not_found
185+
render template: 'errors/not_found', status: :not_found
187186
end
188187

189188
def render_500

app/controllers/errors_controller.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class ErrorsController < ApplicationController
2+
def not_found
3+
render status: :not_found
4+
end
5+
6+
def unacceptable
7+
respond_to do |format|
8+
format.html { render 'public/422', status: :unprocessable_entity }
9+
format.xml { head :unprocessable_entity }
10+
format.json { head :unprocessable_entity }
11+
end
12+
end
13+
14+
def internal_error
15+
respond_to do |format|
16+
format.html { render 'public/500', status: :internal_server_error }
17+
format.xml { head :internal_server_error }
18+
format.json { head :internal_server_error }
19+
end
20+
end
21+
end

app/views/error/not_found.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/views/error/not_found.xml

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/views/error/not_found.html.haml renamed to app/views/errors/not_found.html.haml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
If you don't believe you should be seeing this error,
1010
#{ link_to "please contact us", contact_us_path }!
1111

12-
= render partial: "error/helpful_links"
13-
12+
= render partial: "errors/helpful_links"

app/views/errors/not_found.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

app/views/errors/not_found.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<error>404</error>

config/application.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Application < Rails::Application
3939

4040
config.rakismet.key = ENV['AKISMET_KEY']
4141
config.rakismet.url = ENV['AKISMET_URL']
42+
43+
config.exceptions_app = self.routes
4244
end
4345
end
4446

config/routes.rb

Lines changed: 270 additions & 266 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)