A little routing trickery

Documentation in Rails seems notoriously bad – the documentation appears to be painfully incomplete. Anyway, all the cool kiddies will tell you how to do resource routing; but I wanted a bit more. In particular, i wanted to have URLs that work like http://xxx/things//. Where is the id of the thing you’re watching, and is an additonal identifier that is passed to the controller. For example, you could have a rout that let’s you do http://xxx/cars/bug/green, to show you the green version of the bug.

After a little trial and error, I found that this will be possible using a route like this:

map.resources :cars do |cars|
  cars.connect ':colour', :controller => 'cars',
    :action => 'car_with_colour'
end

Which defines this in a much nicer fashion than matching the route “manually”.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>