Ruby on rails 权威,设计-使用多个设计模型进行授权

Ruby on rails 权威,设计-使用多个设计模型进行授权,ruby-on-rails,devise,pundit,Ruby On Rails,Devise,Pundit,在Rails应用程序中为两个单独的设计模型设置授权。只有当前已登录的医科大学学生才能编辑或删除其个人资料。其他医科学生应该能够查看其他医科学生,普通用户也应该能够查看他们的个人资料 这是我的密码: 政策 class MedicalStudentProfilePolicy attr_reader :medical_student, :medical_student_profile def initialize(medical_student, medical_student_profile)

在Rails应用程序中为两个单独的设计模型设置授权。只有当前已登录的医科大学学生才能编辑或删除其个人资料。其他医科学生应该能够查看其他医科学生,普通用户也应该能够查看他们的个人资料

这是我的密码:

政策

class MedicalStudentProfilePolicy
 attr_reader :medical_student, :medical_student_profile

 def initialize(medical_student, medical_student_profile)
  @medical_student = medical_student
  @medical_student_profile = medical_student_profile
 end

 def edit?
  @medical_student_profile.medical_student_id == medical_student
 end

 def destroy?
  @medical_student_profile.medical_student_id == medical_student
 end
end
权威用户

def pundit_user
 if medical_student_signed_in?
  @medical_student = current_medical_student
 elsif user_signed_in?
  @medical_student = MedicalStudent.find params[:medical_student_id]
 end
end
编辑

 def edit
  authenticate_medical_student!
  authorize @medical_student_profile, :edit?
 end
查看

- if policy(@medical_student_profile).edit?

这在以用户身份登录时有效,但是当前医学院学生无法编辑他们的个人资料。有什么想法吗?

使用角色而不是不同的“用户”类。