Routing Changes in Rails 2.2
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:
[sourcecode language='ruby']
map.resources :rooms, :shallow => true do |room|
room.resources :shelves do |shelf|
shelf.resources : books
end
end
[/sourcecode]
Any of these routes will be recognized:
[sourcecode language='ruby']
/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)
[/sourcecode]
You can also combine shallow nesting with :has_one or :has_many for a more compressed syntax:
[sourcecode language='ruby']
map.resources :rooms, :has_many => { :shelves => :books }, :shallow => true
[/sourcecode]
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:
[sourcecode language='ruby']
map.resources :books, :collection => { :search => [:get, :post] }
[/sourcecode]
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:
[sourcecode language='ruby']
map.resources :rooms, :shallow => true do |room|
room.resources :shelves do |shelf|
shelf.resources : books
end
end
[/sourcecode]
Any of these routes will be recognized:
[sourcecode language='ruby']
/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)
[/sourcecode]
You can also combine shallow nesting with :has_one or :has_many for a more compressed syntax:
[sourcecode language='ruby']
map.resources :rooms, :has_many => { :shelves => :books }, :shallow => true
[/sourcecode]
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:
[sourcecode language='ruby']
map.resources :books, :collection => { :search => [:get, :post] }
[/sourcecode]