2011-08-28 58 views
9

我已經看過類似的帖子,但無法完全得到我需要的答案。設計和STI如何登錄作爲基類註冊

我有一個用戶模型和使用STI學生模型是一種用戶。

當我創建一個新學生時,Devise使用student_session登錄該學生。問題是我的應用程序的其餘部分使用user_session。那麼,我應該使用student_session創建一個新的user_session嗎?然後註銷學生?

或者有沒有辦法讓設計允許學生創建,但作爲用戶基礎模型登錄?

謝謝 安東尼

+3

你有沒有想過這個? – Smickie

回答

2

退房這篇文章,看看它是否可以幫助你:

Rails: Using Devise with single table inheritance

總結基本上是做到以下幾點:

配置/路線。 rb:

devise_for :users, :controllers => { :sessions => 'sessions' }, :skip => :registrations 
devise_for :students, :skip => :sessions 

app/controllers/sessions_controller.rb:

class SessionsController < Devise::SessionsController 
    def create 
     rtn = super 
     sign_in(resource.type.underscore, resource.type.constantize.send(:find, resource.id)) unless resource.type.nil? 
     rtn 
    end 
end 
0

爲此,您必須創建一個自定義控制器。 只是一個示例實施

#POST create 
def create 
    student = Student.create(params[:student]) # normal crud with whatever your form params 
    sign_in(User.find(student.id)) # this actually signs in the user 
    # now redirect the student to the dash board/whatever page manually 
end