2012-10-09 37 views
0

我目前正在構建一個包含3種用戶類型的Rails應用程序。這是我第一次使用網絡開發的經驗,並且我想避免發生嚴重的設計錯誤,這些錯誤將在以後耗費我的成本。希望更有經驗的rails用戶和web開發人員能夠引導我朝着正確的方向發展。Rails應用程序設計,依賴於用戶的路由和內容

我想用設計作爲我的主要認證體系,以支持制定框架內的3用戶類型,我目前正在規劃是這樣的:

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    attr_accessible :email, :password, :password_confirmation, :remember_me 

    belongs_to :rolable, :polymorphic => true 
end 

對於每三個用戶的類型:

# usertype1.rb 
class UserType1 < ActiveRecord::Base 
    has_one :user, :as => :rolable 
end 

# usertype2.rb 
class UserType2 < ActiveRecord::Base 
    has_one :user, :as => :rolable 
end 

基本上,用戶類和幾種不同的用戶類型之間存在多態關聯。我希望這種方法能夠讓我最終在用戶類型模型(例如has-many)中添加不同的關聯關鍵字,以便於查詢數據庫。

我也關心如何實現依賴於用戶的路由。這個想法是,每個用戶類型在登錄時都會看到一個單獨的「中心」,具有不同的儀表板,不同的操作等。我想我會通過重寫Devise SessionsController來解決這個問題。例如:

class SessionsController < ApplicationController 
    def new 
    end 

    def create 
     user = User.find_by_email(params[:email]) 
     if user && user.authenticate(params[:password]) 
     session[:user_id] = user.id 
     if user.type == 1 
      redirect_to hub_typeone 
     else if user.type == 2 
      redirect_to hub_typetwo 
     else 
      redirect_to hub_typethree 
     else 
     flash.now.alert = "Email or password is invalid" 
     render "new" 
     end 
    end 

    def destroy 
     session[:user_id] = nil 
     redirect_to root_url, notice: "Logged out!" 
    end 
end 

這個想法是,成功驗證後,用戶將根據用戶類型路由到不同的頁面。我正計劃使用設計current_user框架來查詢數據庫,以使用特定於用戶的數據填充集線器。

你們有沒有針對我的指針?你有沒有在我的計劃/推理/方法中看到任何巨大的缺陷?提前致謝!

+0

你想出了一個辦法嗎? –

回答

0

我得到一個感覺,你過這個工程,建設東西You Aren't Gonna Need It

,如果你是明確了三種不同的用戶類型是什麼這是最好的。大多數應用程序將需要以下三種用戶類型:

  1. Guest用戶,誰沒有登錄,並且可以訪問應用程序的某些部分
  2. 普通用戶誰管理的常規權利
  3. 管理員用戶用戶。

我很想知道,如果其他用戶類型不在此列表中,您將需要其他用戶類型。

設計wikipages建議如何create a guest userhow to add an admin role。可能最好從實現應用程序常規用戶的功能開始,然後使用上述資源添加其他用戶類型。

+0

感謝Prakash的輸入。但是我不認爲我在過度設計這個。用戶類型對應於現實世界中的不同角色。 – user1639833

相關問題