2016-07-30 2 views

Antwort

3

Schauen Sie sich den Unterschied rake routes verwenden.

Diese Definition mit einem Namespace:

namespace :alpha do 
    resources :posts 
end 

Ergebnisse in den folgenden Routen:

  Prefix Verb URI Pattern      Controller#Action 
    alpha_posts GET /alpha/posts(.:format)   alpha/posts#index 
       POST /alpha/posts(.:format)   alpha/posts#create 
new_alpha_post GET /alpha/posts/new(.:format)  alpha/posts#new 
edit_alpha_post GET /alpha/posts/:id/edit(.:format) alpha/posts#edit 
    alpha_post GET /alpha/posts/:id(.:format)  alpha/posts#show 
       PATCH /alpha/posts/:id(.:format)  alpha/posts#update 
       PUT /alpha/posts/:id(.:format)  alpha/posts#update 
       DELETE /alpha/posts/:id(.:format)  alpha/posts#destroy 

Wie Sie das einzige, was zu sehen, die von einem einfachen resources Leitwegmenge anders ist, ist die Zugabe von /alpha Vorwahl.

Jetzt für die Zwei-Ebenen-resources Routen Definition:

resources :alpha do 
    resources :posts 
end 

die Ergebnisse:

  Prefix Verb URI Pattern        Controller#Action 
    alpha_posts GET /alpha/:alpha_id/posts(.:format)   posts#index 
       POST /alpha/:alpha_id/posts(.:format)   posts#create 
new_alpha_post GET /alpha/:alpha_id/posts/new(.:format)  posts#new 
edit_alpha_post GET /alpha/:alpha_id/posts/:id/edit(.:format) posts#edit 
    alpha_post GET /alpha/:alpha_id/posts/:id(.:format)  posts#show 
       PATCH /alpha/:alpha_id/posts/:id(.:format)  posts#update 
       PUT /alpha/:alpha_id/posts/:id(.:format)  posts#update 
       DELETE /alpha/:alpha_id/posts/:id(.:format)  posts#destroy 
    alpha_index GET /alpha(.:format)       alpha#index 
       POST /alpha(.:format)       alpha#create 
     new_alpha GET /alpha/new(.:format)      alpha#new 
    edit_alpha GET /alpha/:id/edit(.:format)     alpha#edit 
      alpha GET /alpha/:id(.:format)      alpha#show 
       PATCH /alpha/:id(.:format)      alpha#update 
       PUT /alpha/:id(.:format)      alpha#update 
       DELETE /alpha/:id(.:format)      alpha#destroy 

Wie Sie sehen können, wird alpha eine Top-Level-Ressource mit allen 8 RESTful Routen. posts wiederum wird zu einer Ressource zweiter Ebene, die nur über die Route zu einem bestimmten alpha Objekt zugänglich ist.

Lesen Sie mehr in Rails Routing from the Outside In.

Sie könnten auch interessant die scope Option finden. Lesen Sie über den Unterschied zwischen scope und namespace in Scoping Rails Routes Blogpost.

Verwandte Themen