How to create URIs?

From: Stefan Tilkov <>
Date: Tue, 17 Jul 2007 12:35:44 +0200

One of the key constraints - if not *the* key constraint - of REST is
hypermedia. While playing around with the current implementation
during my vacation, I noticed that I find something missing - namely,
a decent way to create links in my representations. The mapping from
URIs to the appropriate class and method, based on the annotations,
works fine - but how do we get back?

The Rails framework offers URI-to-code mapping in both directions.
Here's an example route configuration:

ActionController::Routing::Routes.draw do |map|
   map.customers 'customers/', :controller => 'Customer', :action =>
   map.customer 'customers/:name', :controller => 'Customer', :action
=> 'show'

This creates two "named routes" (called "customer" and "customers").
One effect is that if I run my application on http://localhost:3000,
a GET on a URL like


will create a "CustomerController" object and invoke its "show"
method, passing "whatever" as the value for the name parameter. This
is similar to what can be achieved with the current JSR RI (although
personally, I prefer the centralized, rule-based approach Rails takes).

But Rails also creates a number helper methods, specifically

customer_url(<name>) and

When invoked as customer_url("whatever") and customer_path
("whatever"), these will yield "http://localhost:3000/customers/
whatever" and "/customers/whatever", respectively. With these methods
I can write a collection resource representation (in ERB syntax, very
similar to JSPs) like this:

<% @customers.each do |name| %>
     <li>Link to <a href="<%= customer_path(name) %>"><%= name %></
<% end %>

In contrast, the only way to build links to be included in a
representation using our current API seems to be string
concatenation, duplicating much of the knowledge included in the

In other words: I suggest we extend the API to provide a way to build
a URI based on the resource class and method to be invoked.

For more info on Rails routing, see
classes/ActionController/Routing.html and

Stefan Tilkov,