Skip to content

Instantly share code, notes, and snippets.

@fj
Created May 31, 2012 04:10
Show Gist options
  • Save fj/2840961 to your computer and use it in GitHub Desktop.
Save fj/2840961 to your computer and use it in GitHub Desktop.
DRYer way to do this route-drawing?
MyApp::Application.routes.draw do
resources :fruits
resources :vegetables
resources :sandwiches
resources :desserts
resources :meats
resources :baskets do # DRYer way to do this?
resources :fruits # repeated route! :(
resources :vegetables # repeated route! :(
resources :sandwiches # repeated route! :(
resources :desserts # repeated route! :(
resources :meats # repeated route! :(
end
resources :refrigerators do
# ... repeats again
end
resources :freezers do
# ... repeats again
end
end
@cgriego
Copy link

cgriego commented May 31, 2012

What makes you say this is not DRY?

Or are you looking for the :shallow => true option on resouces?

@fj
Copy link
Author

fj commented May 31, 2012

@cgriego Clarified a bit with an extended example. I'd basically like to be able to take lines 2..6 and put them anywhere else without having to spell it all out.

@fj
Copy link
Author

fj commented May 31, 2012

The approach I'm considering ATM is something like:

def draw_food_routes
  resources :fruits
  resources :vegetables
  resources :sandwiches
  resources :desserts
  resources :meats
end

and then

  draw_food_routes

  [:baskets, :refrigerators, :freezers].each do |r|
    resources r do
      draw_food_routes
    end
  end

@cgriego
Copy link

cgriego commented May 31, 2012

Why is a fruit in a basket different from a fruit in a refrigerator? Is not fruit 42 always fruit 42?

@ahcarpenter
Copy link

MyApp::Application.routes.draw do
  resources :fruits, :vegetables, :sandwiches, :desserts, :meats

  resources :baskets, :refrigerators, :freezers do           # DRYer way to do this?
    resources :fruits, :vegetables, :sandwiches, :desserts, :meats
  end
end

@fj
Copy link
Author

fj commented May 31, 2012

@cgriego That's a good point. Yes, the fruit resource is always the same.

But the information we might want to render in each view is different (e.g. "you're looking in refrigerator #234, which contains fruit #42" would only be shown on /refrigerators/234/fruits/42, and not, say, /baskets/456/fruits/42 or /fruits/42).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment