Rails 2.3.x相当于Rails3的可选路由参数

时间:2021-02-06 11:00:03

In Rails 3 I can do something like this:

在Rails 3中,我可以这样做:

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

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

并且/ page和/ page / some_section都将映射到some_controller #page

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

在Rails 2.3.x中是否有相同的功能?我似乎无法找到它

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

我目前正在使用两种不同的路由方法,如下所示:

map.page          '/page',          :action => 'page'
map.page_section  '/page/:section', :action => 'page'

1 个解决方案

#1


14  

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.

如果:section存在,则该值将是当前值。否则,它将默认为默认值,路由器不会抱怨。

You can also default the value to nil.

您还可以将值默认为nil。

map.page '/page/:section', :action => 'page', :section => nil

#1


14  

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.

如果:section存在,则该值将是当前值。否则,它将默认为默认值,路由器不会抱怨。

You can also default the value to nil.

您还可以将值默认为nil。

map.page '/page/:section', :action => 'page', :section => nil