2016-09-01 70 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

嵌套