Search code examples
ruby-on-rails-3restroutesnested-resourcesruby-on-rails-3.2

DRY in ROR routes description


I have nested combination of routes in json rest application used for different dropdown lists and grouping

 resources :cities, :only =>[:index,:show] 
 resources :regions, :only =>[:index,:show] do
     resources :cities, :only=>[:index, :show] 
 end    
 resources :countries, :only=>[:index,:show] do
   resources :cities, :only=>[:index,:show] 
   resources :regions, :only=>[:index,:show] 
 end

Is there a way to describe it more DRY-way?


Solution

  • If you actually need these routes I think you can't do very much about it. Probably you can just write it in a more concise way using with_options:

      with_options :only => [:index, :show] do |w|
    
        w.resources :cities
        w.resources :regions do
          w.resources :cities
        end
    
        w.resources :countries do
          w.resources :cities
          w.resources :regions
        end
    
      end