2014-01-19 47 views
0

我有嵌套这是这样的路线:嵌套路线和URL帮手轨道4,5

resources :venues do 
    #Halls 
    get "hall/:id/exhibition" => "halls#exhibition", as: :exhibition 
    get "hall/:id/visit" => "halls#visit", as: :hall_visit 
    get "venue_structure", :to => "venues#venue_structure" 
    resources :asset_types, :booths_tags, :tags, :uploaded_files, :events, :chats 
    resources :halls do 
     resources :webcasts 
     resources :booths do 
      resources :chats 
     end 
    end 
end 

问题的这种做法,我不得不把在网址助手嵌套的像下面的第三paramters:

venue_hall_booth_path(@booth.hall.venue, @booth.hall, @booth) 

是否有做得比我这个其他不必每次把三种不同的资源参数,当我用这个帮手更好的方法?

回答

1

您可以使用shallow routes

resources :halls, shallow: true do 
    resources :webcasts 
    resources :booths do 
    resources :chats 
    end 
end 

这允许您访问的网址成员而无需使用父。除非是newcreate操作。

或者你可以单独定义它们。

resources :booths do 
    resources :chats 
end 
resources :halls do 
    resources :webcasts 
    resources :booths 
end 
+0

哪一个是最佳做法? –

+0

这取决于你的需求是什么,但最好不要超过2层。它在各个层面都变得令人头痛,包括测试。想象一下,设置数据以测试控制器的三个级别。 – Mohamad

+0

如果它嵌套了很多,浅层会被更多推荐,因为它只有一个层次? –