【发布时间】:2016-03-23 09:47:06
【问题描述】:
我在这里看到了一些解决方案,但没有一个解决我的问题。我使用 devise 生成了两个模型,即:User 和 Designer。我需要使用 Omniauth 分别为这两个模型注册/登录。目前这是我所拥有的:
用户.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
设计师.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) 我的方法是否正确,或者有没有更好的方法来注册多个模型?
谢谢。请帮忙!
【问题讨论】:
标签: ruby-on-rails ruby devise omniauth