2016-07-30 94 views

回答

3

退房的差异之间的差异资源和命名空间之间的区别。

这个定义与命名空间:

namespace :alpha do 
    resources :posts 
end 

结果如下路线:

  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 

正如你所看到的,是从普通resources路由集,唯一不同的是添加/alpha前缀。

现在的两级resources路线定义:

resources :alpha do 
    resources :posts 
end 

导致:

  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 

正如你所看到的,alpha成为所有8条REST风格的路线顶级资源。反过来,posts成为第二级资源,只能通过路径访问特定的对象。

了解更多Rails Routing from the Outside In

您可能还会感兴趣的scope选项。阅读和namespace之间的区别Scoping Rails Routes博客文章。