简体   繁体   English

Rails多态RESTful路由

[英]Rails Polymorphic RESTful Routing

I have a polymorphic relationship between User, PatientProfile and DoctorProfile. 我在User,PatientProfile和DoctorProfile之间有多态关系。 I'm trying to create RESTful routes for Review . 我正在尝试为Review创建RESTful路由。 What is the best practice for this? 最佳做法是什么?

class User
 belongs_to :profile, polymorphic: true

class PatientProfile
 has_one :user, as: :profile
 has_many :reviews

class DoctorProfile
 has_one :user, as: :profile
 has_many :reviews

class Review
 belongs_to :patient_profile
 belongs_to :doctor_profile

One approach is to create distinct routes for both DoctorProfile and PatientProfile instead of using Users . 一种方法是为DoctorProfilePatientProfile创建不同的路由,而不是使用Users The drawback to this approach is that you would need two reviews controllers DoctorProfileReviewsController and PatientProfileReviewsController : 这种方法的缺点是您将需要两个评论控制器DoctorProfileReviewsControllerPatientProfileReviewsController

resources :patient_profile
  resources :reviews
end
resources :doctor_profile
  resources :reviews
end

What is best way to structure a REST API given these models? 给定这些模型,构造REST API的最佳方法是什么?

\doctors\:id\reviews
\doctors\:id\reviews\:id
\patients\:id\reviews
\patients\:id\reviews\:id
\reviews\:id

Each doctor should be able to have it's reviews queried, each patient should be able to have it's reviews queried, each review should be able to be queried. 每个医生都应该可以查询其评论,每个患者都应该可以查询其评论,应该可以查询每个评论。

Making every resource be accessible is good practice here, if each resource is an active part of the system you'll probably eventually use all of them. 在这里使每个资源都可访问是一种好习惯,如果每个资源都是系统的活跃部分,您最终可能会全部使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM