brad
brad

Reputation: 32335

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'

Upvotes: 8

Views: 1878

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176372

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

Upvotes: 14

Related Questions