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:
[code]script/plugin install git://github.com/ludo/to_slug.git[/code]
Then I started up Rails console to try out my new to_slug method.
[code]
>> "hello world".to_slug
=> "hello-world"
[/code]
Next I wanted to include pretty urls in my routes, so I wrote a spec:
[code]
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
[/code]
After running the spec & getting my expected failure, I added a route:
[code]
map.pretty_course 'courses/show/:id/:slug', :controller => 'courses', :action => 'show'
[/code]
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:
[code]
pretty_course_path(:slug => course.name.to_slug, :controller => 'courses', :action => 'show', :id => course.id)
[/code]