【发布时间】:2012-10-26 00:16:07
【问题描述】:
我有以下操作:
users.rb:
def omniauth_create
auth = request.env["omniauth.auth"]
user = User.from_omniauth(env["omniauth.auth"])
unless user.email.blank?
if user.id.nil?
# Save the user since he hasn't been created yet
user.save!
end
sign_in user
redirect_back_or user
else
# Send user to a form to fill his email
#session[:omniauth] = request.env['omniauth.auth'].except('extra')
redirect_to(enter_email_path(oprovider: user.provider,
ouid: user.uid,
oname: user.name,
opassword: user.password,
opassword_confirmation: user.password))
end
end
它执行以下操作:
- 如果用户的
email不为空,请让他登录,然后将他重定向到他的个人资料(如果他的id是nil,则保存他。换句话说,如果他还没有被创建) . - 如果用户的
email为空白,请将其发送至enter_email_path(用户可以在其中输入他的电子邮件)。
现在我想添加另一个 if 语句,如果 email 已被占用,则会闪烁错误,并将用户重定向到 root_path
我不太确定该怎么做,有什么建议吗? (以及将 if 语句放在哪里?)
编辑:
奇怪,得到了这个而不是重定向到根路径:
Validation failed: Email has already been taken
我不知道这是否有帮助,但这是 from_omniauth 的来源:
def self.from_omniauth(auth)
find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
end
def self.create_with_omniauth(auth)
new do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.email = auth["info"]["email"]
user.password = user.password_confirmation = SecureRandom.urlsafe_base64(n=6)
end
end
现在的代码:
user.rb:
# if user.email.present?
if user.id.nil?
# User.find_by_email(user.email).present?
if User.exists?(:email => user.email)
redirect_to root_path
end
user.save!
end
sign_in user
redirect_back_or user
else
(其余不变)。
似乎代码忽略了if User.exists?(:email => user.email) 部分?
【问题讨论】:
标签: ruby-on-rails ruby if-statement omniauth