2016-03-23 7 views
0

私はここでいくつかの解決策を見てきましたが、誰も私の問題に答えていません。私は、UserとDesignerの2つのモデルを生成するためにdeviseを使いました。そして、Omniauthを使ってこの2つのモデルを別々にサインイン/サインインする必要があります。現在、これは私が持っているものです。Devise + Omniauth複数のモデル

User.rb

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
    user.provider = auth.provider 
    user.uid = auth.uid 
    user.user_name = auth.info.name 
    user.email = auth.info.email 
    user.password = "password" 
    user.skip_confirmation! 
end 

user_authentications_controller.rb

class UserAuthenticationsController < Devise::OmniauthCallbacksController 

def create 
    begin 
     @user = User.from_omniauth(request.env['omniauth.auth']) 
     sign_in_and_redirect @user 
     #redirect_to root_url, notice: "Signed in!" 
     flash[:success] = "Welcome, #{@user.first_name}!" 
     UserMailer.welcome(@user).deliver_now 
     rescue 
     flash[:warning] = "There was an error while trying to authenticate you..." 
     end 
    end 
end 

designer.rb

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |designer| 
    designer.provider = auth.provider 
    designer.uid = auth.uid 
    designer.user_name = auth.info.name 
    designer.email = auth.info.email 
    designer.password = "password" 
    designer.skip_confirmation! 
    end 
end 

designer_authentication_controller.rb

DesignerAuthenticationsController < Devise::OmniauthCallbacksController 


    def create 
    begin 
     @designer = Designer.from_omniauth(request.env['omniauth.auth']) 
     sign_in_and_redirect @designer 
     #redirect_to root_url, notice: "Signed in!" 
     flash[:success] = "Welcome, #{@designer.first_name}!" 
     UserMailer.welcome(@designer).deliver_now 
     rescue 
     flash[:warning] = "There was an error while trying to authenticate you..." 
     end 
    end 
end 

routes.rbを

devise_scope :user do get "/auth/:provider/callback" => "user_authentications#create" end 
devise_scope :designer do get "/auth/:provider/callback" => "designer_authentications#create" end 

私の問題は、次のとおりです。

1)どんなにがどの私はサインアップページ、設計者やユーザーから、それがユーザーとして登録されます。両方のサインアップにuser_authentications_controllerを使用しているからです。どのようなアイデアをどのように私は彼らがサインアップページで呼び出すコントローラを決定させることができますか?

2)私は正しいやり方をしていますか、複数のモデルにサインアップする方法はありますか?

ありがとうございました。そして助けてください!

答えて

0

なぜ2つのモデルが必要ですか?あなたは同じ認証を使用して、役割を通じてユーザーまたはデザイナーを区別することができます。私はいくつかの鉄道放送のビデオを見て、さまざまなテクニックのスピードアップに取り組んでいます。カンカンと

役割: http://railscasts.com/episodes/192-authorization-with-cancan

ウォッチこの1つは、独自の認証を書く方法についての感覚を得るために: http://railscasts.com/episodes/250-authentication-from-scratch-revised

1

私はここに私の経験を共有しましょう、私はあなたと同じことを行っていました現在行っている。問題は、両方のユーザーのすべての手順を実行していない可能性があります。すくいルートを行い、両方のユーザーとデザイナーのための工夫によって作成されているか別のルートを確認し、それに応じてそれを呼び出す、この後

1) rails generate devise MODEL (user and designer) 

2) rails generate devise:controllers [scope] (users and designers) 

3) rails generate devise:views USER (users and deigners) 

-

はここで従うべき手順の概要です。

私の知る限り、コードベースが後で非常に乱雑になるので、異なるユーザーを作成するよりも役割を使用する方が良いです。ビルドを始めたばかりの場合は、ロールが先に進む方法です。