2010-02-26 63 views
1

我的路线失控是由于这样的事实,这是不可能的(纠正我,如果我错了)整齐地定义多个资源:路线 - 与定制

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

由于嵌套东西航线正在失去控制(整洁)。

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 

我怎样才能避免这种重复:

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

是否有某种方式来存储路径作为一个块的地方,或者定义,在我的情况,english_pages和西班牙语页面别名seperately?

非常感谢。

回答

1

所有你需要的是一点元编程。

资源只是一个方法调用,而这些符号最终被评估为字符串,所以这变得非常容易。

[: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

非常感谢,应该知道这将是简单的事情。 – mark 2010-02-26 16:04:56