| Path: | README |
| Last Update: | Fri Jul 21 21:43:51 CEST 2006 |
This plugin makes slugs for your URLs. Does not require an extra field in the table.
In the models that you want to make sluggable, do this:
class Article < ActiveRecord::Base
acts_as_sluggable :with => 'title'
end
Where ‘title’ is the name of the attribute from which to extract the slug. Now, whenever you do something like this:
link_to 'Read article', :action => 'show', :id => @article
You will get URLs like this:
/articles/show/76-omg-my-cat-is-so-cute-lol
When using url_for and friends, :id is a special parameter. If you pass a model instance as the :id, it will send to_param to that instance, which defaults to something like id.to_s. This plugin overrides to_param to produce a string consisting of the ID and your attribute of choice, separated by a dash. The attribute is processed to remove any strange characters before it is returned. This process can be overridden by creating a make_slug method on your models.
Additionally, some ActiveRecord internals are overridden to extract the ID again before sending it to the database.
Inspiration and the basic workings of this was lifted from here:
www.notsostupid.com/blog/2006/07/07/urls-on-rails/
Thanks :)