Created
May 31, 2012 04:10
-
-
Save fj/2840961 to your computer and use it in GitHub Desktop.
DRYer way to do this route-drawing?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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.
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
Why is a fruit in a basket different from a fruit in a refrigerator? Is not fruit 42 always fruit 42?
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
@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
What makes you say this is not DRY?
Or are you looking for the
:shallow => true
option onresouces
?