Ruby on rails Rational在RESTful URL中具有嵌套资源的父id
给定RESTful API中嵌套资源的以下URL设计:Ruby on rails Rational在RESTful URL中具有嵌套资源的父id,ruby-on-rails,ruby,rest,url,restful-url,Ruby On Rails,Ruby,Rest,Url,Restful Url,给定RESTful API中嵌套资源的以下URL设计: /magazines/:magazine_id/ads/:id POST 考虑到广告id唯一地标识杂志中的广告,那么将杂志id放在那里的原因是什么 此外,当向用户展示该URL或简单的约定时,它看起来可能更好。还有什么更深层次的含义或限制吗?嗯,这在很大程度上取决于谁在发展。理论上,没有必要 事实上,Rails指南在中显示了这一点,即您只能在资源没有id时嵌套资源: resources :articles do resources
/magazines/:magazine_id/ads/:id POST
考虑到广告id唯一地标识杂志中的广告,那么将杂志id放在那里的原因是什么
此外,当向用户展示该URL或简单的约定时,它看起来可能更好。还有什么更深层次的含义或限制吗?嗯,这在很大程度上取决于谁在发展。理论上,没有必要 事实上,Rails指南在中显示了这一点,即您只能在资源没有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]
鉴于广告id在杂志上唯一标识广告
这是最普遍的惯例,但不是普遍的惯例。您可以在模型中将重写为_param
,并违反数据库主键约定的通用性。想象一下,你也为杂志做了这件事(例如,为了SEO目的)。在这种情况下,在路线中包括刀库ID/slug可能是非常必要的。这正是“浅”的作用:
意思和
resources :magazines do
resources :ads, only: [:index, :new, :create]
end
resources :ads, only: [:show, :edit, :update, :destroy]
请参见第2.7.2节中的浅嵌套
resources :magazines do
resources :ads, only: [:index, :new, :create]
end
resources :ads, only: [:show, :edit, :update, :destroy]