Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I merge route declarations (subdomain or token)?

I have a model Model that can be access from many ways: by subdomain or a token

  • http://model1.domain.com
  • http://domain.com/j4h7

I have the following 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

So I currently have to duplicate all the routes for the two cases.

Is there any way to declare it only once?

like image 239
efji Avatar asked Apr 25 '11 13:04

efji


1 Answers

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

Related topic: Rails 3 Routes: DRY members

like image 126
fl00r Avatar answered Oct 25 '22 02:10

fl00r