使用Devise / omniauth
考虑以下模型
class User < ActiveRecord::Base
has_many :usertracks, :dependent => :destroy
mount_uploader :image, ArtistPhotoUploader
TEMP_EMAIL_PREFIX = 'change@me'
TEMP_EMAIL_REGEX = /\Achange@me/
# Include default devise modules. Others available are:
# :lockable, :timeoutable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update
validates_presence_of :image
validates_integrity_of :image
validates_processing_of :image
def self.find_for_oauth(auth, signed_in_resource = nil)
# Get the identity and user if they exist
identity = Identity.find_for_oauth(auth)
user = signed_in_resource ? signed_in_resource : identity.user
# Create the user if needed
if user.nil?
email_is_verified = auth.info.email && (auth.extra.raw_info.verified || auth.info.verified || auth.extra.raw_info.primary_email_confirmed)
email = auth.info.email if email_is_verified
firstname = auth.info.name.split(' ')[0].capitalize
surname = auth.info.name.split(' ')[1].capitalize
if email
user = User.where("email = ?", email).first
else
user = User.where("firstname = ? AND surname = ?",firstname, surname)
end
# Create the user if it's a new registration
if user.blank?
user = User.new(
firstname: firstname,
surname: surname,
#username: auth.info.nickname || auth.uid,
email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
password: Devise.friendly_token[0,20],
remote_image_url: auth.info.image.gsub('http://','https://')
)
user.oauth_token = auth.credentials.token
user.oauth_secret = auth.credentials.secret
user.skip_confirmation!
user.save!
else
# if user found, update credentials - token/secret to overwrite previous provider
p "this is the user #{user.inspect}"
user.oauth_token = auth.credentials.token
user.oauth_secret = auth.credentials.secret
user.update!
end
end
# Associate the identity with the user if needed
if identity.user != user
identity.user = user
identity.save!
end
user
end
def email_verified?
self.email && self.email !~ TEMP_EMAIL_REGEX
end
end
由于某些原因,即使找到了用户,我也会在标题中收到错误
这是问题的确切部分
# if user found, update credentials - token/secret to overwrite previous provider
p "this is the user #{user.inspect}"
user.oauth_token = auth.credentials.token
user.oauth_secret = auth.credentials.secret
user.update!
示例数据库条目,这是它在错误行之前在控制台中打印的用户。
#<User id: 16, email: "change@me-16740355-soundcloud.com", encrypted_password: "$2a$10$OtCdj.7M2cWu8eOYXNc5QeWUZdrcESds
YY47WRYBfZt...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, curre
nt_sign_in_at: "2016-04-13 14:34:56", last_sign_in_at: "2016-04-13 14:34:56", current_sign_in_ip: "127.0.0.1", last_sign_i
n_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2016-04-13 14:34:55", confirmation_sent_at: nil, unconfirmed_em
ail: nil, created_at: "2016-04-13 14:34:55", updated_at: "2016-04-13 14:34:56", firstname: "Petros", surname: "Kyriakou",
image: "avatars-000015072102-sqpyn9-large.jpg", oauth_token: "1-234832-16740355-50cdc35302cdd4", oauth_secret: nil>
答案 0 :(得分:1)
错误发生是因为在这一行
user = User.where("firstname = ? AND surname = ?",firstname, surname)
我没有返回一个对象,而是一个活动的关系对象。
改为此工作,
user = User.where("firstname = ? AND surname = ?",firstname, surname).first
将其留待此处以供将来参考