Logo
Talk to Rails Conf Europe 2006

Title:
======
"All You Ever Wanted To Know About Routing And Never Dare To Ask"

Abstract:
=========
Designing clean, nice and semantically significant URL is important to build services which are remarkable and easy to discover and mash-up.
Rails supports this through the Routing module which offers a simple yet effective default behavior based on simple conventions, the module is also flexible enough to allow the development of more complex setups through a simple URL mapping domain specific language.
In this talk we'll see the use of routing through many examples, covering also the tools available to test and debug it.
    
Description:
============
Designing applications to succeed and give users an enticing experience while remaining usable and simple to understand requires attention to details. URLs should have a clear and precise meaning in the context of the application and should be easily guessed to increase usability and ease the building of RESTful web services on top of the data.

In this talk we'll see how the Rails routing module implements a simple but effective domain specific language to help in designing the public face of a site by mapping URL to executable code (namely controllers and actions).
These mappings are entirely defined using ruby code (in the routes.rb configuration file) and are platform agnostic (they work alike on any combination of O.S. and web server supporting Rails).

Routes come preconfigured on application generation and offer a simple and consistent initial setup which doesn't require any coding to be used. In case it's needed, the developer is free to tweak the routing setup adding custom mappings.

Each mapping consists of a path definition and a set of options. The path definition describes the url pattern handled by the mapping i.e. how the path should be decomposed in segments and how each segment should be used to build the parameter hash which is then passed to the application controller.

A path segment can define a symbol which will be used as a key in the parameter hash, the value for that key will be extracted from the actual request path using dynamically generated regular expressions. Special symbols are used to drive the request to the correct controller and action.

Routes can also be given names and in this case Rails defines special helper functions to ease the burden of defining those urls inside the application code.

We'll also see how the mapping behaviour can be heavily customized by the definition of various options, including the ability to specify a custom  regular expression to recognize (and extract) the path segment. 
Many examples of complex routes will be given, taking them from actual products or defining them specifically for the presentation purposes.

Designing complex route mappings can become difficult to handle without adequate debugging and testing support, thus the testing methods specific for the routing and built into rails will be presented.
The excellent routing navigator plug-in will also be shown as a way to debug the mappings while using a live application.

Then an overview on the internals of the Routing module will be presented with the aim of showing how the routing module might be extended to allow more complex route definition or to make simpler the definition of complex routes by extending the definition language. 

We'll see the basics of how the process of route handling is split in the generation and recognition phases and what actions are taken to map a request path into the parameter hash.
The extension points of the module (extract_request_environment and recognition_conditions) will be presented and the code from an actual plug-in will be examined to show how easy it is to add new behaviours. 

All of the talk will be based on the new routing code as implemented in edge rails.