如何为访问同一资源的 2 类用户创建路由? [英] How to create routes for 2 types of users accessing the same resource?

查看:40
本文介绍了如何为访问同一资源的 2 类用户创建路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天我一直在努力模拟这个.我有两种类型的用户:doctorshospitals.医院可以发布病例,医生可以索赔.

I've been banging my head over the past few days trying to model this. I have 2 types of users: doctors and hospitals. Hospitals can post cases and doctors can claim them.

doctor has_many caseshospital has_many casescases own_to doctorcases own_to Hospital.

他们都有一个profile.这是他们可以选择查看他们拥有的案例的起点.医院只能看到他们发布的病例.医生只能查看所有无人认领的病例和他们目前正在审查的病例.

Both of them have a profile. That's the starting point where they can choose to view the cases they have. Hospitals can only see the cases they posted. Doctors can only see all unclaimed cases and the cases they are currently reviewing.

如何设计我的路由以 REST 方式有意义"?

How do I design my routes to "make sense" RESTfully?

这是我建议的 routes.rb:

Here is my proposed routes.rb:

resource :doctor_profile, only: :show do            #=> Show doctor profile
    resources :cases, only: [:index, :show, :edit] #=> See 1 or all cases, edit case
end

resource :hospital_profile, only: :show do
    resources :cases, only: [:create, :index, :show, :edit]
end

还有几个问题:

  1. 他们共享控制器吗?我只是假设他们有自己的控制器.
  2. 如何让他们共享相同的路线但显示不同的内容?例如:医生访问时的个人资料/案例"= 查看他们的盘子,而医院访问时的个人资料/案例"= 查看他们的盘子.
  3. 我是否还要创建一个cases_controller?
  4. CanCan 是一个很好的用例吗?

老实说,这感觉就像 2 个应用访问 1 个数据库.它应该是这样的吗?(我认为这与电子商务相似,在电子商务中,买家与卖家的看法不同).因此,非常欢迎任何推荐的资源.

This is honestly feeling like 2 apps accessing 1 database. Is it supposed to be like this? (I think this is parallel to e-commerce where a buyer sees things differently from a seller). So any recommended resources are very welcome.

推荐答案

通常我会在这个场景中做的是创建这个控制器结构

Generally what I would do in this scenario is create this controller structure

/app/controllers/doctors_controller.rb
/app/controllers/doctors/cases_controller.rb

/app/controllers/hospitals_controller.rb
/app/controllers/hospitals/cases_controller.rb

然后,对于嵌套的每个目录,需要将控制器包裹在一个模块中.例如在 doctors/cases_controller.rb

Then, for each directory of nesting, you need to wrap the controller in a module. For example in doctors/cases_controller.rb

module Doctors
  class CasesController < ApplicationController

  end
end

现在您可以创建您的 routes.rb

resources :doctors do 
  resources :cases, controller: 'doctors/cases', only: [:index, :show] do 
    post '/claim', action: 'claim'
  end
end

您还可以考虑为处理基本功能的案例添加一个根级控制器,两个嵌套的 cases_controller 继承自这些功能.这是我喜欢放置适用于两个控制器的授权方法的地方

You also might consider adding a root-level controller for cases that handles the basic functions, which the two nested cases_controllers inherit from. This is the place I like to put authorization methods that apply to both controllers

# app/controller/cases_controller.rb
class CasesController < ApplicationController

end

这篇关于如何为访问同一资源的 2 类用户创建路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆