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 =>
'index'
map.customer 'customers/:name', :controller => 'Customer', :action
=> 'show'
end
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
http://localhost:3000/customer/whatever
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
customer_path(<name>)
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:
<h1>Index</h1>
<ul>
<% @customers.each do |name| %>
<li>Link to <a href="<%= customer_path(name) %>"><%= name %></
a></li>
<% end %>
</ul>
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
annotations.
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
http://api.rubyonrails.org/
classes/ActionController/Routing.html and
http://api.rubyonrails.org/
classes/ActionController/Resources.html
Stefan
--
Stefan Tilkov, http://www.innoq.com/blog/st/