sjsc
sjsc

Reputation: 4652

Ruby on Rails: Is it possible to DRY up routes?

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)?

Upvotes: 0

Views: 119

Answers (1)

Shadwell
Shadwell

Reputation: 34784

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

Upvotes: 6

Related Questions