我的应用程序有两个用户,doctors和hospitals。医生可以查看病例并对其进行诊断,而医院则为医生提供诊断案例。所有这些都是从他们自己的用户配置文件中完成的。
路线设置的方式是: doctor_profile_path:去看医生的个人资料。hospital_profile_path:去医院的侧写。
我希望profile_path重定向到用户的配置文件,不管他们有什么帐户。这样,当他们是医院的时候,他们也不能去doctor_profile_path,而且路径名称更简洁。我该怎么做?
其他问题:
routes.rb。我试着添加路径:“配置文件”,但是Rails只会找到第一个可用的匹配程序,这样它就不能工作了
resource :doctor_profile, only: :do do
resources :plates, controller: 'doctor/cases', only: [:index, :show] do
post '/claim', action: 'claim'
end
end
resource :hospital_profile, only: :do do
resources :cases, controller: 'hospital/cases', only: [:new, :create]
end控制器结构如下:
doctor/
-> cases_controller #=> contains the actions a doctor can do with a case
-> (things the doctor can do on his profile)
hospital
-> cases_controller #=> contains the actions a hospital can do with a case
-> (things a hospital can do on his profile)
profile_controller.rb(这是如何为访问同一资源的两种类型的用户创建路由?的延续)
发布于 2014-03-06 15:04:46
def profile
@user = current_user
end 在ProfilesController中
并加入routes
get '/profile' => 'profiles#profile' ,这将为您提供路径:profile_path,并使用它构建一个链接<%= link_to 'profile', profile_path %>
现在您可能希望显示不同类型的信息,如果user是doctor或hospital,那么在views/profiles/profile.html.erb中您可以这样做
<%= render partial: "#{@user.is_doctor? ? 'doctor' : 'hospital'}" %>这将需要app/views/profiles中的两个部分
1 _doctor.html.erb
2 _hostpital.html.erb如果您有user.profile_type属性:
def is_doctor?
self.profile_type == 'doctor'
end发布于 2014-03-06 15:12:09
您可能只需要一个用户模型,但角色不同(医生/医院)。以下是你可以开始的地方:
它们都可以使用身份验证框架(如设计,设计 )。
更新:
我已经在聊天中回答了一些关联概念。在这里,我只是提供一个例子来解决你的问题,在我看来。
您不需要“两种”的用户模型,相反,使用一个具有不同角色的单一用户模型,一切就变得简单了。您所需要的只是处理用户,例如,单个用户登录页面和单个用户配置文件页面,并在需要时根据用户角色显示不同的部分。下面是一个示例:
您可能需要在用户模型中添加一个role列来判断用户是哪个角色(这里我只想简化示例),并添加一些用于角色控制的方法(参见下面的用户模型)。请注意,在创建用户时,应指定一个角色。
class CreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
# ...
# NOTE: here is a simple example to implement a single role
# management(i.e., one role only for each users), if you
# need more complex management, please consider using
# a role management gem, such as rolify.
# role: 0 = normal user, 1 = doctor, 2 = hosiptal
t.integer :role, null: false, default: 0
# ...
end
end
end但是医生和医院的特殊领域呢?我将创建另外两个模型-- DoctorInfo和HospitalInfo,用于为不同的角色存储这些字段,并创建一个关联(属于)用户模型。如果用户的角色是医生,则有一个doctor_info;如果他/她的角色是医院,则有一个hospital_info。
class CreateDoctorInfos < ActiveRecord::Migration
def change
create_table(:doctor_infos) do |t|
# ...
t.belongs_to :user
# ...
# columns for a doctor role only
# ...
end
end
end
class CreateHospitalInfos < ActiveRecord::Migration
def change
create_table(:hospital_infos) do |t|
# ...
t.belongs_to :user
# ...
# columns for a hospital role only
# ...
end
end
end
class User < ActiveRecord::Base
has_one: hospital_info
has_one: doctor_info
# ...
ROLE_MAP = {
normal_user: 0,
doctor: 1,
hospital: 2
}
validates :role, inclusion: { in: ROLE_MAP.values }
def is_a_role?(role_code)
(self.role == ROLE_MAP[role_code])
end
end
class DoctorInfo < ActiveRecord::Base
belongs_to: user
# ...
end
class HospitalInfo < ActiveRecord::Base
belongs_to: user
# ...
end如果您想在用户配置文件视图中显示特定的字段,最简单的方法就是rmagnum2002在他的回答中提到的。
<div>
To show the profiles for all kinds of roles.
</div>
<% @user.is_a_role?(:doctor) %>
<div>
To show the particular information for a user who is a doctor.
You may need something like this: @user.doctor_info.xxx
</div>
<% end %>
<% @user.is_a_role?(:hospital) %>
<div>
To show the particular information for a user who is a hospital.
You may need something like this: @user.hospital_info.xxx
</div>
<% end %>“告诉你别问”怎么样?再一次,取决于你的需求。是否每个角色之间的角色行为如此不同,需要一个多态性(我的意思是纯红宝石类多态性,而不是模型多态性)。注意,角色行为是代码级的实现,没有数据。)来对付他们?还是只是一些页面上的显示差异?实际上是另一个故事。
https://stackoverflow.com/questions/22223608
复制相似问题