Even better looking URLs with permalink_fu
Extracted from Mephisto is the handy plugin called permalink_fu.
This plugin allows you to use ID-less permalinks, much like that in this blog.
I wrote earlier about how to use to_param for nicer URLs, but these are even better. The plugin automatically turns a field in your model into something that could be a permalink, by stripping out all non english and non alphanumeric letters. It then saves this in a field in your database for future use.
So, onto the HowTo:
First, install the plugin in your rails app.
./script/plugin install http://svn.techno-weenie.net/projects/plugins/permalink_fu/
Now you need to add a field to your database to store the permalink.
./script/generate migration add_permalink_to_article
In the migration we add the field called permalink.
add_column :designers, :permalink, :string
Now for the model code
class Article < ActiveRecord::Base
- title is the field name you want to convert to a permalink
has_permalink :title
- you can also specifiy a different permalink field in your database by giving a second paramater
- has_permalink :title, :my_permalink_field
- we now add the to_param method which Rails’s routing uses
def to_param
permalink
end
end
The permalink field is only populated after the record is validated, so if you have a bunch of records already in your database you can get the permalink field to populate with
Article.find(:all).each(&:save)
If you already have lots of view code written then this is the easy way to get these routes working – in your controller :
@article = Article.find_by_permalink(params[:id])
However that doesn’t seem to pass the smell test using :id rather than :permalink, but will work fine.
To use :permalink you will have to change your routing code and your view code :
# in your route file
map.connect 'article/:permalink', :controller => 'article', :action => 'view'
# in your views when linking
link_to "View #{article.title}", {:controller => 'designer', :action => 'view', :permalink => article.permalink}
# then in your controller you can use
@article = Article.find_by_permalink(params[:permalink])
Thats all! I’m always on the lookout for SEO related Rails topics to talk about so drop me a line @ questions at this domain .com.
