views:

31

answers:

1

I have the nested resource "resources :comments" added to a lot of parent resources as follows (using Rails 3):

resources :cusines do
  resources :comments
end

resources :recipes do
  resources :comments
end

resources :chefs do
  resources :comments
end

resources :countries do
  resources :comments
end

etc., etc., etc.

Since I have about 10 similar ones like the above, I figure it's not very DRY. Is it possible to DRY up my routes somehow that nests the comments resource for all needed resources (that is, so I don't have to type "resources :comments" every time)?

+4  A: 

You could iterate over the various things you want comments on and define the resources like:

[:cuisines, :countries, :recipes, :chefs].each do |r|
  resources r do
    resources :comments
  end
end
Shadwell
Awesome! Thanks so much for the tip Shadwell :)
sjsc