2011-08-28 7 views
9

私はすでに同じような投稿を見ましたが、私が必要としていた答えは得られませんでした。deviseとSTIのログイン方法は登録時にBaseクラス

私はUserモデルを持っており、STIをUserタイプのStudentモデルを使用しています。

私は新しい生徒を作成するときに、その生徒にstudent_sessionでログインします。問題は、私のアプリケーションの残りの部分がuser_sessionを使用していることです。だから、私はstudent_sessionを使って新しいuser_sessionを作成すべきですか?学生をログアウトしますか?

Deviseに学生の作成を許可する方法がありますが、ユーザーベースモデルとしてログインしますか?

は、 をありがとうアンソニー

+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