Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a 404 instead of 500 in Rails

In my rails app I have defined the routes so users can access records like http://mydomain.com/qwe2

But if they type a wrong "qwe2" they get a 500 page. I think a 404 would be more appropriate.

How can I change the error page that is shown? Thanks

like image 469
Victor Avatar asked Mar 31 '10 13:03

Victor


1 Answers

Create a catch-all route at the bottom of config/routes.rb:

map.connect '*path', :controller => 'unknown_route'

Then in app/controllers/unknown_route_controller you can do:

class UnknownRouteController < ApplicationController
  def index    
    render :file => "#{Rails.root}/public/404.html", :layout => false,
           :status => 404
  end
end

This will render your 404 page for any unknown routes.

like image 192
John Topley Avatar answered Oct 04 '22 18:10

John Topley