Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In my Ruby on Rails application I want to show 404 error page instead of routing error when the given route does not matches or exists in my application. Can anybody help me to make it possible?

like image 938
HONy Avatar asked Sep 13 '11 10:09

HONy


People also ask

How do I display 404 page in react?

Every Website needs a 404 page if the URL does not exist or the URL might have been changed. To set up a 404 page in the angular routing, we have to first create a component to display whenever a 404 error occurred. In the following approach, we will create a simple react component called PagenotfoundComponent.

How do I add a 404 page to my react router?

An alternative way to handle 404 page not found in React router is to redirect the user to a different page when they try to go to a page that doesn't exist. Copied! In the example, instead of rendering a PageNotFound component, we redirect the user to / if they go to a route that doesn't exist. Copied!

How do I provoke a 404 error?

The typical trigger for an error 404 message is when website content has been removed or moved to another URL. There are also other reasons why an error message could appear. These include: The URL or its content (such as files or images) was either deleted or moved (without adjusting any internal links accordingly)


2 Answers

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

like image 53
cluesque Avatar answered Oct 05 '22 21:10

cluesque


This return 404 page

In ApplicationController

class ApplicationController < ActionController::Base

      rescue_from ActiveRecord::RecordNotFound,    with: :route_not_found
      rescue_from ActionController::RoutingError,  with: :route_not_found
      rescue_from ActionController::UnknownFormat, with: :route_not_found


    def route_not_found
        render file: Rails.public_path.join('404.html'), status: :not_found, layout: false
      end
like image 29
sparkle Avatar answered Oct 05 '22 23:10

sparkle