Created
November 1, 2011 11:20
-
-
Save mahemoff/1330350 to your computer and use it in GitHub Desktop.
Slug/Permalink using Rails 3 Concern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example (untested) | |
class Post | |
include Slugged | |
end | |
post.create :title=>'Great Gist' | |
post.slug == 'great-gist' # true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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