How can I show a 404 error page instead of a routing error?

In my Ruby on Rails application, I want to show a 404 error page instead of a routing error when this route does not match or exists in my application. Can someone help me make this possible?

+6
source share
4 answers

This is already the default behavior in production. The development environment displays routing errors so that the developer can notice and correct them.

If you want to try, start the server in production mode and check it.

$ script/rails s -e production 
+11
source

in ApplicationController

  rescue_from ActiveRecord::RecordNotFound, :with => :rescue404 rescue_from ActionController::RoutingError, :with => :rescue404 def rescue404 #your custom method for errors, you can render anything you want there end 
+6
source

If you cannot easily start production mode locally, set consider_all_requests_local to false in the config/environments/development.rb file.

+4
source

You can catch an exception that is thrown when a route is not found, and then displays a custom page. Let me know if you need help with the code. There can be many other ways to do this, but it definitely works.

0
source

Source: https://habr.com/ru/post/897221/


All Articles