Johnny Zapata
Johnny Zapata

Reputation: 19

How to modify Rails error pages from the public directory with Haml

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?

Upvotes: 1

Views: 470

Answers (1)

whatapalaver
whatapalaver

Reputation: 915

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:

Upvotes: 1

Related Questions