开发者

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 开发者_StackOverflow社区:comments" every time)?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜