1

I'm trying to customize the error pages from public directory using Haml.

When I go to the route localhost:3000/404 it shows:

'Routing Error'

and I don't understand why. Can someone help me to understand why this happens?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Welcome to Stack Overflow. There's no need to apologize for being unexperienced, just follow the guidelines for asking questions in "[ask]" and the linked pages and "[mcve](https://stackoverflow.com/help/minimal-reproducible-example)". Unfortunately we need more information. What have you done? When did it break? Imagine you'd been given the information on a piece of paper by a coworker, who then walked away. What would you need to know to help? Then edit the question and provide that information as if it'd always been there; Don't put in "EDIT" or "Update" tags as we can tell what's changed. – the Tin Man Jul 19 '19 at 23:37

1 Answers1

1

It sounds like you are amending the 404.html file within the public directory and renaming it to 404.html.haml.

This will prevent Rails from being able to find it in the default routing.

If you wish to apply a custom error page I would recommend "Dynamic Rails Error Pages" which proposes the following steps:

  1. Generate a controller and views for the custom errors:

    rails generate controller errors four_oh_four_not_found five_hundred_error
    
  2. Ensure the correct status codes are sent otherwise Rails will send a 200 status code:

    class ErrorsController < ApplicationController
      def four_oh_four_not_found
        render(:status => 404)
      end
    
      def five_hundred_error
        render(:status => 500)
      end
    end
    
  3. Configure the routes:

    match "/404", :to => "errors#four_oh_four_not_found"
    match "/500", :to => "errors#five_hundred_error"
    
  4. Tell Rails to use our custom routes for the error pages:

    config.exceptions_app = self.routes
    
  5. Delete the default 404.html and 500.html views in public.

There are quite a few StackOverflow questions that deal with error pages in Rails such as:

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
whatapalaver
  • 865
  • 13
  • 25
  • 1
    IMO you should avoid that. the reason is when your rails server is not accessible the error pages won't be accessible. and once you delete the default pages from public directory you will end up with the ugly 404 and 500 pages – Deepak Mahakale Jul 19 '19 at 16:38