Posted by: Brian | February 28, 2008

Making Ruby on Rails SEO URL’s from a ColdFusion developers perspective

Here is a little tip for those who are used to ColdFusion and don’t know all of the Ruby syntax yet.

In ColdFusion, one can sort on a list and change the delimiter. Well, Ruby has no list functions (I am open to correction with this). What is one to do?

For the Langorang site, I am working on a complete redesign from the ground up to incorporate Search Engine Optimization (SEO) techniques into the site. The thing I was working on last night was the SEO URL’s. Or, for the layperson, pretty URL’s.

This looks like the following:

http://www.link-to-your-site.com/?id=78965432

When looking at that link, one has no idea what id= 78965432 points to. So to make it pretty, a developer who is working with SEO in mind may make it look more like this.

http://www.link-to-your-site.com/story/seo-is-the-way-to-go-for-pretty-urls

Without going into all of the details, this makes the site much easier for the human to read and for the computer to parse.

With this in mind I had to write a permalink system and still have the ability to serve up content based on a unique id. The idea was to add the unique id to the permalink and then sort it out in the code later. The nice thing about the permalink is that it has a natural delimiter with the dashes.

For Example
Permalink: nice-seo-permalink-generated
With Unique ID: 007-nice-seo-permalink-generated

In ColdFusion I would use a listGetAt() after determining how long the list was with a listLen() and setting the delimiter to ‘-‘.

Ruby has a little tool called split (which I absolutely love).

Split works like this:
yourlist.split(“your delimiter”) in my case lstSeoUrl.split(“-”)

The power becomes apparent when you go to call out the split items. In my case the first variable is the ID I want to use to return the data. So by calling lstSeoUrl.split(“-”)[0] (note in ColdFusion the first variable in an array would start with 1 not 0), I am now able to return just the number for the ID and continue with processing.

After finding this out, my SEO life got a little easier and hopefully this will help someone else out. Now all I have to do is to complete the rewrite and get Langorang relaunched before the beginning of the month. Wish me luck!! I’ll need it!!


Responses

  1. I have been searching for hours for this info! Very helpful!

  2. glad I could help.

  3. You are probably working too hard; Rails will automatically convert a URL from example.com/articles/7-nice-seo-permalink-generated to example.com/articles/7, via the type conversion of the :id parameter that automatically happens.

    So, all you need to do is add one method to your model, e.g.:

    class Article < ActiveRecord::Base
    def to_param
    “#{id}-#{title.parameterize}”
    end
    end

    After that, everything will just work.

    Google for “ruby on rails permalink urls” for much more detail, and some Rails plugins that can make this even easier.

    • Hey thanks for the input, but I wrote this for those who have to use a ColdFusion environment. I already use rails and love it. Moved to it a long time ago. However, there are plenty of people out there that have to use something else. I think it is better to help them out where possible and not act all superior with my current favorite technology.

      Thanks for the post.


Leave a comment

Categories