【问题标题】:Adding custom :new routes using Rails 3 routing使用 Rails 3 路由添加自定义 :new 路由
【发布时间】:2010-06-15 11:04:29
【问题描述】:
在 Rails 2 中,我们可以为资源丰富的路由添加自定义 new 操作,例如:
map.resources :users, :new => {:apply => :get}
我们如何在 Rails 3 中实现同样的目标?
resources :users do
get :apply, :on => :new # does not work
new do
get :apply # also does not work
end
end
有什么想法吗?
【问题讨论】:
标签:
ruby-on-rails
routing
ruby-on-rails-3
【解决方案1】:
您可以在边缘路由指南中将:path_names 用作explained:
resources :users, :path_names => { :new => "apply" }
这只会将路径更改为apply,它仍将被路由到new 操作。我认为不再明确支持更改(这可能是一件好事)。
如果您想保留 apply 操作,您可能应该这样做:
resources :users, :except => :new do
collection do
get :apply
end
end
但这让您想知道将apply 操作重命名为new 是否更好。
【解决方案2】:
试试这个:
resources :users, :path_names => { :new => 'apply' }
请注意,如果您想将所有路由的 new 路由重新映射到 apply,那么您可以使用范围:
scope :path_names => { :new => 'apply' } do
# The rest of your routes go here...
end