开发者

Routes - Tidily define multiple resources with customisation

My routes are getting out of hand due开发者_开发百科 to the fact that this isn't possible (correct me if I'm wrong):

map.resources :english_pages, :as => :english, :spanish_pages, :as => :spanish do |article|

Due to nested routes things are spiralling out of control (tidiness).

map.resources :english_pages, :as => :english, :member => {:manage => :get} do |article|
  article.resources :topics do |topic|
    topic.resources :posts
  end
end
map.resources :spanish_pages, :as => :spanish do |article|
  article.resources :topics do |topic|
    topic.resources :posts
  end
end

How can I avoid duplication of this:

article.resources :topics do |topic|
  topic.resources :posts
end

Is there some way to store routes as a block somewhere, or perhaps define, in my case, english_pages and spanish pages aliases seperately?

Thanks very much in advance.


All you need is a little bit of meta-programming.

resources is just a method call, and the symbols end up being evaluated as strings anyway, so this becomes pretty easy.

[:english, :spanish].each do |language|
  map.resource "#{language}_pages", :as => language, :member => {:manage => :get} do |article| 
    article.resources :topics do |topic|
    topic.resources :posts
  end
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜