如何合并路由声明(子域或令牌)?

时间:2011-04-25 13:35:29

标签: ruby-on-rails subdomain constraints routes

我有一个可以通过多种方式访问​​的模型模型:通过子域或令牌

我有以下路线

resources :model, :constraints => {:model_id => /[a-zA-Z0-9]{4}/} do
  ... (nested resources...)
end
resources :model, :constraints => {:subdomain => /.+/} do
  ... (same as above: nested resources...)
end

所以我目前必须复制这两种情况的所有路线。

有没有办法只声明一次?

1 个答案:

答案 0 :(得分:4)

def nested_routes
  get :some_route
  post :some route
  resources :some_resources
end

resources :model, :constraints => {:model_id => /[a-zA-Z0-9]{4}/} do
  nested_routes
end

resources :model, :constraints => {:subdomain => /.+/} do
  nested_routes
end

相关主题:Rails 3 Routes: DRY members