Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created November 1, 2011 11:20
Show Gist options
  • Save mahemoff/1330350 to your computer and use it in GitHub Desktop.
Save mahemoff/1330350 to your computer and use it in GitHub Desktop.
Slug/Permalink using Rails 3 Concern
# example (untested)
class Post
include Slugged
end
post.create :title=>'Great Gist'
post.slug == 'great-gist' # true
# stringex gem in Gemfile
# "slug" and "title" attributes in models where you want slugs
# t.column "title", :string
# t.column "slug", :string
module Slugged
extend ActiveSupport::Concern
included do
before_save :slugify
end
def slugify
self.slug = self.title.to_url # to_url is provided by stringex gem.
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment