2016-09-01 77 views
0

鉴于一个RESTful API以下URL设计嵌套的资源:理性具有REST风格的URL嵌套资源的父ID背后

/magazines/:magazine_id/ads/:id POST 

什么是合理的具有在那里杂志ID鉴于广告背后ID uniquiely标识跨杂志广告?

除此之外,它呈现与该网址或者干脆约定的用户时看起来也许更好。有没有更深的含义或约束?

回答

0

嗯,这取决于谁发展非常多。理论上,没有必要。

事实上,Rails的指南显示这个在2.7.2 Section (Shallow Nesting),你只能窝资源时,他们没有一个id:

resources :articles do 
    resources :comments, only: [:index, :new, :create] 
end 
resources :comments, only: [:show, :edit, :update, :destroy] 

或者你的情况:

resources :magazines do 
    resources :ads, only: [:index, :new, :create] 
end 
resources :ads, only: [:show, :edit, :update, :destroy] 
0

鉴于广告ID uniquiely跨越杂志标识的广告吗?

这是最常见的惯例,但不是万能的。你可以自由地覆盖模型to_param和违反普遍性逐数据库主键惯例。想象一下,你也为杂志做了这些(例如,为了SEO目的)。在这种情况下,在路线中包括杂志ID/slug可能是非常必要的。

0

这正是 “浅” 的作用:

resources :magazines do 
    resources :ads, shallow: true 
end 

意味着完全一样

resources :magazines do 
    resources :ads, only: [:index, :new, :create] 
end 
resources :ads, only: [:show, :edit, :update, :destroy] 

参见2.7.2节浅在http://edgeguides.rubyonrails.org/routing.html

嵌套