404 error pages in Rails

I’m always trying to encourage visitors to my website to find the right information. I’m also pretty lazy with typing and want to be able to just guess at URL’s and see if they work.

The new website system for Sustainable Websites, built in Ruby on Rails, has articles (called posts in WordPress) and pages. I wanted the page not found (HTTP status 404) page to automatically direct them to the appropriate page, if it exists, or article, and if it doesn’t exist, provide some suggested links and a search box (pre-filled with some keywords from the URL they tried).

The initial part of this was based on Recipe 7 (page 43) of:“Advanced Rails Recipes: 84 New Ways to Build Stunning Rails Apps (Pragmatic Programmers)” (Mike Clark)


The first part was setting up a default route in routes.rb:

# 404 handler
map.connect '*path', :controller => 'four_oh_fours'

then here’s the 404 controller:

class FourOhFoursController < ApplicationController
   def index
   #clean the slash out of request.path.
   cleaned_path = request.path.gsub(/\//," ").strip
   #if request.path is in the set of page permalinks then redirect to that page
   to_page = Page.find_by_permalink(cleaned_path)
   if to_page then
     redirect_to page_permalink_url(to_page.permalink) and return false
   end
   #same if it is an article name
   to_article = Article.find_by_permalink(cleaned_path)
   if to_article then
     redirect_to permalink_url(to_article.permalink) and return false
   end

   #TODO: same if it a username?

   FourOhFour.add_request(request.host, request.path, request.env['HTTP_REFERER'] || '')
   respond_to do |format|
     format.html { render :file => "#{RAILS_ROOT}/public/404.html", :status => "404 Not Found" }
     format.all { render :nothing => true, :status => "404 Not Found" }
   end
   end
end

It uses the Page model’s find_by_permalink method but I could have easily just used find by title or another attribute of Pages.

Tags: , , , ,

Leave a Reply

My Web Host

Subscribe

  • Subscribe via Feed Burner
  • Subscribe in Yahoo
  • Add to Google
  • Add to My AOL

Twizeets!

My Travels


Chat with me


click-to-call from the web

Google Friend Connect

Flickr

www.flickr.com

My Music