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...
Add to your Gemfile:
gem 'acts_as_slugable', :git => '[email protected]:kickstarter/acts_as_slugable.git'
In your target table, add a column to hold the URL slug.
class Page < ActiveRecord::Base
acts_as_slugable :source_column => :title, :slug_column => :slug, :scope => :parent
end
class Post < ActiveRecord::Base
acts_as_slugable :source_column => :title, :slug_column => :slug
end
link_to @page.title, :action => 'show', :slug => @page.slug
# app/models/page.rb
class Page
def to_param
slug
end
end
# config/routes.rb
resources :pages
# app/controllers/pages_controller.rb
def index
@page = Page.create(:name => "my page")
end
# app/views/pages/index.html.erb
<%= url_for @page # generates "/pages/my-page" %>
The unit tests for this plugin use an in-memory sqlite3 database.
To execute the unit tests run the default rake task (rake). To execute the unit tests but preserve to debug log run rake test.
Created by Alex Dunae, 2006-07, though it takes a village to raise a plugin:
Thanks to Andrew White for fixing a conflict with acts_as_list.
Thanks to Philip Hallstrom for pointing out some redundant code.