Agile Web Development

Build it. Launch it. Love it.

acts_as_slugable

Generates a URL slug based on a specific fields (e.g. title).

A url slug is a string derived from a specific field which can the be used in a URL. For instance, a page with the title My page would have a URL slug of my-page.

The slug is generated on save and create actions. If the field has an existing URL slug (like when editing an existing record) the URL slug is preserved.

URL slugs are unique within the specified scope (or all records if no scope is defined). If the slug already exists within the scope, -n is added (e.g. my-page-0, my-page-1, etc…

Installation

./script/plugin install http://code.dunae.ca/acts_as_slugable

Usage examples

In your target table, add a column to hold the URL slug.

With scope

  class Page < ActiveRecord::Base
    acts_as_slugable :source_column => :title, :target_column => :url_slug, :scope => :parent
  end

Without scope

  class Post < ActiveRecord::Base
    acts_as_slugable :source_column => :title, :target_column => :url_slug
  end

A sample link

  link_to @page.title, :action => 'show', :url_slug => @page.url_slug

Testing

The unit tests for this plugin use an in-memory sqlite3 database (http://www.sqlite.org/).

To execute the unit tests run the default rake task (rake). To execute the unit tests but preserve to debug log run rake test.

Credits

Created by Alex Dunae (dunae.ca), 2006-07

Vitals

Home http://code.dunae.ca/acts_as_slugable.html
Repository http://code.dunae.ca/acts_as_slugable/
License Rails' (MIT)
Tags Tag_red address chunk jlk permalink slug stub uri url
Rating (11 votes)
Owner Alex Dunae
Created 20 November 2006

Comments

  • Avatar
    27 November 2006

    What does this solve that the existing acts_as_sluggable plugin doesn't?

  • 9 December 2006

    Re: Tom Fakes' comment...

    The plugins are similar. This version doesn't append or prepend the record ID, a format I have never liked.

    This plugin also allows scoping.

  • 8 June 2009

    The option that specifies the column to store the slug is ignored

Add a comment