Here I present a couple way to add custom 404 Not Found pages to your Rails app.
This is particularily useful so that your not found pages get rendered with the applications layout and styles. Also helps add some minor security so that users cannot tell that it is a Rails app based on the default error page.
Common Code
### app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def render_404
if request.format.html?
render "exceptions/not_found", status: 404
else
render plain: "404 Not Found", status: 404
end
end
end
### app/views/exceptions/not_found.html.erb
<h1>404 Not Found</h1>
Routing Option A
### config/routes.rb
match '*path', to: 'application#render_404', via: :get
Routing Option B
### config/application.rb
config.exceptions_app = self.routes
### config/routes.rb
match '/404', to: 'application#render_404', via: :get