Thursday, February 28, 2008

Route Globbing

The default route "map.connect ':controller/:action/:id'" is sufficient in most of the cases when dealing with URLs and routing. It matches most of the common URLs that are coming your way. But what if we need an URL like: which might mean:

There is something called as Route Globbing to solve this problem. In your routes.rb file specify the following line:
map.connect 'year/month/*values'

Routing engine bundles all the values after the third slash into params[:values] as an array. So your year/month action will have access to this array. The params[:value] will now look like ["5","28","18","30","20"].

If we want another controller to receive the URL, then we can say:
map.connect 'year/month/*values', :controller => "accounts"

Route globbing could be helpful if we are allowing users to enter some kind of organizational structure or file paths in to the URL.

No comments: