Easiest Permalink/PrettyURLs on Rails

This is the simplest way I’ve found to get pretty permalinks and/or URLs for this blog (for SEO optimization): use a 37signals-like standard prefix the id of the post to the prettyfied version of the title of the blog. Use the following to Find the post:

 

def self.find_by_permalink(permalink)
  return(nil) unless permalink
  self.find(permalink.split("-")[0])
end

 

Use the following to build the permalink:

 

def permalink
    "#{self.id}-#{self.title.gsub(/[^a-z0-9]+/i,'-')}"
end