Test First SEO: Pretty Urls with Rails and to_slug Plugin

I used to do search engine marketing before making the career switch to software engineering and I’ve been doing some work on classes.blazingcloud.net to help Google figure out what we are all about. The contents of the url is one of the many ways to improve Google’s understanding of a webpage. ‘Pretty Urls’ is search engine optimization (SEO) lingo for semantic urls that describe in words what the page is about. In my quest to prettify our urls, I found the to_slug Rails plugin. I ran the plugin install:

script/plugin install git://github.com/ludo/to_slug.git

Then I started up Rails console to try out my new to_slug method.

>> "hello world".to_slug
=> "hello-world"

Next I wanted to include pretty urls in my routes, so I wrote a spec:

  describe "Routes" do
    it "for Course#show should have pretty urls" do
      route_for(:controller => "courses", :action => "show", :slug => "hello-world", :id => 1).should == "/courses/show/1/hello-world"
    end
  end

After running the spec & getting my expected failure, I added a route:

map.pretty_course 'courses/show/:id/:slug', :controller => 'courses', :action => 'show'

My test passed, so I went on to update the links. I can now use the following code and it will go to my desired pretty url:

pretty_course_path(:slug => course.name.to_slug, :controller => 'courses', :action => 'show', :id => course.id)

Post a Comment

Your email is never shared. Required fields are marked *

*
*