我目前正在構建一個包含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框架來查詢數據庫,以使用特定於用戶的數據填充集線器。
你們有沒有針對我的指針?你有沒有在我的計劃/推理/方法中看到任何巨大的缺陷?提前致謝!
你想出了一個辦法嗎? –