Search Engine Friendly Links in Rails

If you haven’t read the book “Practical Rails Social Networking Sites” by Alan Bradburne yet, there’s a chapter there which discusses how you can create a simple content management system and create permanent links for every page created.

Adding the following code to the model file:

def to_param
  "#{id}-#{permalink}"
end

object.to_param would result to:

/01-hello-world

Not cool because one of the purposes of using permalinks or friendly URL’s is to get rid of incomprehensible data or information that the search engine doesn’t need. The id is only needed by the rails application and not google or some search engine.

The ideal permalink should be:

yoursitedomain/hello-world

I still think the book written by Alan Bradburne is interesting. However there’s a need to update for Rails 2. ActiveRecord and almost everything has changed but not drastically of course.

To achieve friendlier links for pages, posts, categories and even user links of your application, there are a lot of options in Rails. I have only tried two rails plugins and fortunately didn’t spend too much time on it.

First option: permalink fu

cd my_app 
script/plugin install git://github.com/technoweenie/permalink_fu.git

The implementation is fairly simple. If you have a model Class named Post, you can try the following.

class Post < ActiveRecord::Base
  has_permalink :title
end

Herryanto Siatono wrote a plugin that extends permalink fu. I have experienced some problem after installing the plugin but I could not verify until I do some extensive tests. It seems like I got a hash instead of english words downcased after adding a new post.

Another problem encountered is the permalink fu plugin doesn’t really account for post updates. If the “Hello World” post title is updated to “The World is Flat,” the link should be “the-world-is-flat.” However that is not the case and I didn’t want to bother finding ways to make it happen with permalink fu. It could have been done with the callback before_save but if someone else who have struggled to make permalink fu work could enlighten on how it was done, it would be great.

Second option:

This gem saved my life today. It’s very intuitive, accounts for non-unique slug names and appends a number on the friendly id and best of all, it accounts for updates.

Make sure you check out the rake tasks relevant for this plugin. (rake –tasks)

rake friendly_id:make_slugs          # Make slugs for a model.
rake friendly_id:redo_slugs          # Regenerate slugs for a model.
rake friendly_id:remove_old_slugs    # Kill obsolete slugs older than 45 days.

All slug names are not automatically deleted after update. Instead, they are all stored in the database table “slugs”. This may not be a good thing of course. But instead of focusing on the could-have-been’s, we focus on what is and appreciate the author for his efforts. Probably after a day, you could do

rake:friendly_id:remove_old_slugs MODEL=Post DAYS=1

Conclusion: I have only tried two options and I have no idea if there’s any better solution than friendly id but I think the rails community in general should come up with a unified approach regarding clean urls. Writing another plugin may not be such a good idea.

Interesting read: