Skip to content

Commit 9035ebf

Browse files
committed
Merge pull request #249 from codebender/fix/custom_404_page
Display custom 404 page: invalid route / record not found - Bounty #428
2 parents 73fe695 + fe4e15b commit 9035ebf

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.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "rails_helper"
2+
3+
feature "Custom 500 Page" do
4+
before(:all) do
5+
Rails.application.config.action_dispatch.show_exceptions = true
6+
Rails.application.config.consider_all_requests_local = false
7+
end
8+
9+
after(:all) do
10+
Rails.application.config.action_dispatch.show_exceptions = false
11+
Rails.application.config.consider_all_requests_local = true
12+
end
13+
14+
scenario 'User is presented 500 page when an exception is raised' do
15+
allow(User).to receive(:find_by_username!).and_raise(StandardError)
16+
17+
visit '/user_causes_500_error'
18+
19+
expect(page).
20+
to have_content('Coderwall had an issue but hold on to your localhosts')
21+
end
22+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "rails_helper"
2+
3+
feature "Custom 404 Page" do
4+
before(:all) do
5+
Rails.application.config.action_dispatch.show_exceptions = true
6+
Rails.application.config.consider_all_requests_local = false
7+
end
8+
9+
after(:all) do
10+
Rails.application.config.action_dispatch.show_exceptions = false
11+
Rails.application.config.consider_all_requests_local = true
12+
end
13+
14+
scenario 'user is presented 404 page when they visit invalid path' do
15+
visit '/fake/path/doesnt/match/route'
16+
17+
expect(page).to have_content('Uh oh, something went wrong!')
18+
end
19+
20+
scenario 'user is presented 404 page when then visit a bad user path' do
21+
visit '/not_a_real_username'
22+
23+
expect(page).to have_content('Uh oh, something went wrong!')
24+
end
25+
end

0 commit comments

Comments
 (0)