For the most part, routing is unchanged in the upcoming Rails 2.2 release. But there are two changes that add a little extra syntactic sugar to your routes.
First, as Ryan Daigle covered last month, there’s a new :shallow option for nested routes. This change goes a long way to answer the classic objections to deeply-nested routes. What shallow nesting does is give you additional route helpers. For example, with this declaration:
map.resources :rooms, :shallow => true do |room|
room.resources :shelves do |shelf|
shelf.resources : books
end
end
Any of these routes will be recognized:
/rooms/1/shelves/2/books/3 ==> room_shelf_book_path(1,2,3) /shelves/2/books/3 ==> shelf_book_path(2,3) /books/3 ==> book_path(3)
You can also combine shallow nesting with :has_one or :has_many for a more compressed syntax:
map.resources :rooms, :has_many => { :shelves => :books }, :shallow => true
The second change is that you can now supply an array of methods for new member or collection routes on resources. With Rails 2.1, you had to either pick a method or use the wildcard :any method. With Rails 2.2, you can specify just the verbs that a route really needs:
map.resources :books, :collection => { :search => [:get, :post] }


2 comments
Comments feed for this article
October 7, 2008 at 4:50 pm
Morgan Roderick
Oh sweet joy!
Finally! you might add :-)
It’s often the smallest changes, or polish, that takes a product from good to great, and Rails continue to deliver polish, while still evolving the framework.
November 9, 2008 at 7:48 am
Mike Gunderloy
A note to those looking at the description of shallow routes above: part of it is WRONG. Shallow routes do not provide additional helpers, they limit the helpers to the ones that supply the necessary information, and remove the others. So, of the routes I show, only book_path(3) actually works.
Mea culpa.