Skip to content

Instantly share code, notes, and snippets.

@raphaelcm
Created October 11, 2011 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphaelcm/1278700 to your computer and use it in GitHub Desktop.
Save raphaelcm/1278700 to your computer and use it in GitHub Desktop.
Easy way to support custom (human-written) URLs (helpful with SEO) using friendly_id
class BlogPost < ActiveRecord::Base
# This Model has, in addition to title and other usual fields,
# two fields for creating custom urls:
#
# :use_custom_url, :boolean
# :custom_url, :string
validates :title, :presence => true, :uniqueness => true
has_friendly_id :title_or_custom, :use_slug => true
protected
def title_or_custom
use_custom_url ? custom_url : title
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment