添加自定义:使用Rails 3路由的新路由

在Rails 2中,我们可以为资源丰富的路由添加自定义new操作,例如:

 map.resources :users, :new => {:apply => :get} 

我们如何在Rails 3中实现同样的function?

 resources :users do get :apply, :on => :new # does not work new do get :apply # also does not work end end 

有任何想法吗?

您可以使用:path_names如边缘路由指南中所述:

 resources :users, :path_names => { :new => "apply" } 

这只会改变应用的路径,它仍将被路由到new动作。 我认为不再明确支持改变(这可能是一件好事)。

如果您想保留您的apply行动,您应该:

 resources :users, :except => :new do collection do get :apply end end 

但它让你想知道将apply操作重命名为new是否更好。

试试这个:

 resources :users, :path_names => { :new => 'apply' } 

请注意,如果要重新映射new路由以apply所有路由,则可以使用范围:

 scope :path_names => { :new => 'apply' } do # The rest of your routes go here... end