Eric Bergman-Terrell's Blog

Ruby on Rails 3.1.1: Creating Custom 404 Pages
November 23, 2011

I've spent way too much time trying to replace the default "404" page for this rails app. From what I've learned, the mechanism to trap "url not found" issues changed in Rails >= 3, and may be broken. At least I wasn't able to get custom, dynamic, "404" pages to work.

One thing that *almost* worked was adding a low-priority routing rule to the very bottom of my routes.rb file, that targeted the controller and view for an error page that I created. Doing that did cause my custom "404" page to display, but it also broke the ability of the administrator to log on to the site.

The only suggestion that I found that actually worked 100% was a suggestion to generate a static 404 html file using rake. The following Rakefile uses the curl command to retrieve the markup for my 404 page, and it stores it in /public/404.html. The page is not dynamic, but it's at least more attractive than the default.

task :default do
  print "Updating 404 page\n"
  `curl --silent http://localhost:3000/error_404 -o ../public/404.html`
end

This solution is adapted from from Tony Edgecombe's stackoverflow answer.

If it is actually possible to trap "url not found" conditions in Rails 3.1.1, please feel free to submit a comment explaining how. Thanks!

Keywords: Rails, Rails 3.1.1, 404, url not found, custom error pages

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015