Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 2.3.x equivalent of Rails3 optional route parameters

In Rails 3 I can do something like this:

match "/page(/:section)", :to => 'some_controller#page'

And both /page and /page/some_section will map to some_controller#page

Is there an equivalent of this in Rails 2.3.x ?? I can't seem to find it

I'm currently using two separate route methods like so:

map.page          '/page',          :action => 'page'
map.page_section  '/page/:section', :action => 'page'
like image 816
brad Avatar asked Dec 08 '10 16:12

brad


1 Answers

A parameter becomes optional if you specify a default value.

map.page '/page/:section', :action => 'page', :section => "default"

If :section is present, the value will be the current value. Otherwise, it will default to default and the router won't complain.

You can also default the value to nil.

map.page '/page/:section', :action => 'page', :section => nil
like image 108
Simone Carletti Avatar answered Sep 21 '22 17:09

Simone Carletti