別擔心,你不會被卡住。
我認爲你最好的選擇是重寫Devise的會話控制器並改變會話的'new'和'create'方法。
因此,在你自己的「sessions_controller.rb」,你會有這樣的事情:
class SessionsController < Devise::SessionsController
# GET /resource/sign_in
def new
resource = build_resource(nil, :unsafe => true)
clean_up_passwords(resource)
respond_with(resource, serialize_options(resource))
end
# POST /resource/sign_in
def create
resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_in_path_for(resource)
end
end
而在你的路線,你會使用的東西(這取決於你命名你的用戶模型),如:
devise_for :users, :controllers => {
:sessions => 'sessions'
}
devise_for :businesses, :controllers => {
:sessions => 'sessions'
}
上述會話控制器根本沒有自定義。我不知道你的代碼,所以我真的不能幫助。但基本步驟如下:
- 檢查
auth_options
和resource_name
變量以瞭解它們如何存儲數據。
- 添加您的條件邏輯來改變這些變量,如果
resource
沒有在用戶表中找到。
設計很棒,角色很棒,但有時候有一個用戶模型,甚至使用STI,都沒有意義。因爲我在最近的一個項目中處理了這個問題,所以我很快在這個問題上寫了一篇較長的文章。
我同意。現在轉到「角色」的方式,在太多時間過去之前,項目需要第三個模型。 – Zabba