It’s time for something new

After 14 years we’re hanging up our keyboards. Our team is joining the lovely people at Culture Amp, where we’ll be helping build a better world of work.

Icelab

Tasteful Routes Ruby Gem

By Hugh Evans07 Jun 2011

Crafting clean URLs is an essential part of modern web development and a telling measure of the quality of your web application. With the size of the web now so massive a lot of users are starting to discriminate simply on the design of URLs (I know I certainly do). Ultimately this means unless your site’s URLs are up to scratch then incoming links may simply be passed by for no other reason. Additionally, with more and more rubbish content turning up in search engine results an overly tuned for SEO URL will probably be equally discriminated against. A savvy net user will recognise that a very long URL is a common trait of a content farm.

My general rule of thumb is that if a URL looks like it was generated by a machine then it simply isn’t good enough.

In this vein I recently noticed that Github had employed an interesting URL pattern for its newly released Pull Requests 2.0, which I’m assume is powered by Ruby on Rails. The index is at /pulls as you’d expect, however the show action is in the format of /pull/1. Here’s an example. I find this reads better than the Rails default of /pulls/1 and seems more human oriented.

Inspired by this pattern I created a Ruby Gem for Rails 3 that gives you singular member action routes. It’s designed to be a simple drop-in replacement for the standard Rails RESTful routes resources method and supports all the same options, including nesting.

YourApp::Application.routes.draw do
  tasteful_resources :jobs
end

This will give you the following routes:

    jobs GET    /jobs(.:format)         {:controller=>"jobs", :action=>"index"}
         POST   /jobs(.:format)         {:controller=>"jobs", :action=>"create"}
 new_job GET    /job/new(.:format)      {:controller=>"jobs", :action=>"new"}
edit_job GET    /job/:id/edit(.:format) {:controller=>"jobs", :action=>"edit"}
     job GET    /job/:id(.:format)      {:controller=>"jobs", :action=>"show"}
         PUT    /job/:id(.:format)      {:controller=>"jobs", :action=>"update"}
         DELETE /job/:id(.:format)      {:controller=>"jobs", :action=>"destroy"}

A side benefit is that it becomes easier to intercept show and index actions with jQuery in your progressively enhanced application. The trailing slash is optional in rails and as such the selector regex needs to compensate. With tasteful routes it’s much simpler:

$('a[href^="/jobs"]') // collection
$('a[href^="/job/"]') // members

Install tasteful_routes from rubygems.org and let me know what you think.