2014-03-06 86 views
1

我的應用程序有兩個用戶,doctorshospitals。醫生可以查看病例並對其進行診斷,醫院可以爲醫生創建病例以供診斷。這些都是從他們自己的用戶配置文件完成的。如何將不同的控制器映射到Rails中的1條路徑?

路線設置的方式是這樣的:doctor_profile_path:轉到醫生的檔案。 hospital_profile_path:去醫院的個人資料。

我想profile_path重定向到用戶的個人資料,無論他們擁有哪個帳戶。這樣他們在醫院時也不能去doctor_profile_path,路徑名稱更簡潔。我該怎麼做呢?

其他問題:

  1. 如何實現授權?
  2. 視圖文件夾應該如何構建?

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 

(這是How to create routes for 2 types of users accessing the same resource?的延續)

+0

你在使用設計嗎?如果是這樣,醫生和醫院都使用相同的用戶資源登錄? – rmagnum2002

+0

感謝您回覆@ rmagnum2002。對。這兩種賬戶只需一次認證。我應該使用兩個? –

+0

加入此聊天http://chat.stackoverflow.com/rooms/49149/http-stackoverflow-com-questions-22223608-how-can-i-map-different-controllers-t – rmagnum2002

回答

1
def profile 
    @user = current_user 
end 

ProfilesController

,並添加routes

get '/profile' => 'profiles#profile' 

,這將使你的路徑:profile_path,並建立與之<%= link_to 'profile', profile_path %>

現在一個鏈接,你可能希望顯示不同種類的信息,如果user是一個doctorhospital 所以在views/profiles/profile.html.erb你會做

<%= render partial: "#{@user.is_doctor? ? 'doctor' : 'hospital'}" %> 

,這將需要2分音在app/views/profiles

1 _doctor.html.erb 
2 _hostpital.html.erb 

,如果你有user.profile_type屬性:

def is_doctor? 
    self.profile_type == 'doctor' 
end 
+0

嘿,我檢查了這一點。那麼[告訴別問](http://robots.thoughtbot.com/tell-dont-ask)呢?我覺得我們可以在條件語句分成多態性 –

+1

良好的網頁,但似乎這個頁面說,有更好的方法可以做到,不是最好的,我不知道是否有渲染的方法2個不同頁面的方式如果你明白我的意思。你可以在配置文件頁面中完全跳過這個if/else語句,如果你不會在頁面上有太多差異並且使用類方法告訴你不要問:-) – rmagnum2002

0

您可能只需要一個用戶模式,但不同的角色(醫生/醫院)。以下是您可以開始的地方:

  • rolify - 定義用戶模型的角色。
  • cancan - 定義每個角色可以做什麼。

他們都可以使用身份驗證框架,如devise

UPDATE:

我已經回答聊天某種關聯的概念。在這裏,我只是提供一個例子來解決我的問題。

您不需要「兩種」用戶模型,而是使用具有不同角色的單個用戶模型,那麼一切都變得簡單。您只需要處理用戶,例如單個用戶登錄頁面和單個用戶配置文件頁面,並根據用戶角色(如果需要)顯示不同的部分。以下是一個示例:

您可能需要在用戶模型中添加一個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 

但是對於醫生和醫院的具體領域呢?我會創建另外兩個模型 - DoctorInfoHospitalInfo來爲不同角色存儲這些字段,並建立關聯(屬於)用戶模型。如果他/她的角色是醫生,則用戶具有一個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 %> 

「告訴別問」怎麼樣?再次,取決於您的要求。每個角色之間的角色行爲是如此不同,並且需要一個多態性(我的意思是純ruby類多態,而不是模型多態。注意角色行爲是代碼級的實現,沒有關於數據的內容)。處理它們?或只是在一些頁面顯示差異?這實際上是另一回事。

+0

Thanks @Sibevin。如果我和2個用戶模型一起使用會怎麼樣?我有這個原因態關聯去有醫生有醫院不反之亦然 –

+0

如果他們是如此不同,你需要兩個模型來處理他們,我想他們應該是獨立的,有自己的控制器和領域視圖(如分隔的登錄/個人資料頁面)。這是一個非常罕見的案例,我看到的唯一用例是用戶和管理員。 –

+0

如果醫生/醫院模型之間只有幾個不同的領域,我仍然建議使用一個角色的用戶模型,但增加兩個模型,例如doctor_info和hospital_info。不同的字段存儲在這兩個模型中,並且這些模型與(belongs_to)用戶模型相關聯。如果角色是醫生,則用戶有一個doctor_info,如果角色是醫院,則有一個hospital_info。 –

相關問題