这是我的模特
class User < ActiveRecord::Base
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
这是我的控制器
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_url
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
end
如果有任何重复条目,我尝试查看数据库。但不是。如果你能提供帮助,请告诉我。
答案 0 :(得分:0)
只需将where(条件)更改为:
where(provider: auth.provider, uid: auth.uid)
由于方法#permitted,第一种方法失败了? AR调用(如果已定义)来清理属性。
> h = auth.slice(:provider, :uid)
> h.class
=> OmniAuth::AuthHash < Hashie::Mash
> h.permmitted?
> false
但是,一个简单的Hash不会有#permitted?定义,因此它将继续:
> h = { provider: auth.provider, uid: auth.uid }
> h2.permitted?
NoMethodError: undefined method `permitted?' for {:provider=>"facebook", :uid=>"XXXX"}:Hash