Route with optional args?

Is it possible to specify a route like this:

/items(/category/:category_id)(/order/:order)(/page/:page)

and have any of the parts in parentheses be optional? I know I can make
:category_id and the other
vars optional, but is it possible with the entire
“/category/:category_id” part?

thanks
csn


Yahoo! Mail - PC Magazine Editors’ Choice 2005

On 18.11.2005, at 11.07, CSN wrote:

Is it possible to specify a route like this:

/items(/category/:category_id)(/order/:order)(/page/:page)

and have any of the parts in parentheses be optional? I know I can
make :category_id and the other
vars optional, but is it possible with the entire “/
category/:category_id” part?

You can only make variables optional from the tail end. So in
“/:a/:b/:c” either :c, :c and :b or all :c, :b and :a can be
optional. However, just :a or just :b cannot be optional.

Why? Because there’s no way for Rails to know whether your url
meant :a/:b or :a/:c.

However, what you want to accomplish is possible, albeit with
multiple routes:

map.connect ‘/items/category/:category_id/order/:order/
page/:page’, :controller => ‘foo’, :action => ‘bar’
map.connect ‘/items/order/:order/page/:page’, :controller =>
‘foo’, :action => ‘bar’
map.connect ‘/items/page/:page’, :controller => ‘foo’, :action => ‘bar’
and so on and so forth…

Kind of awkward, that’s true. But afaik there’s no way around it if
you want that even parts in the middle of an url are optional.

//jarkko