Forum: Ruby on Rails route with optional args?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-18 10:09
(Received via mailing list)
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
http://mail.yahoo.com
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2005-11-18 10:21
(Received via mailing list)
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
This topic is locked and can not be replied to.