我尝试使用google帐户管理用户注册我的rails 4.0.0应用。设计完美。现有用户可以使用Google帐户登录。但是我使用Google Oauth 2进行新用户注册时遇到了一些困难。例如:我有谷歌帐户“example@google.com”。它已登录到我当前的PC上。当我尝试使用此帐户注册我的应用程序时,它会生成空白注册表单。如果我不手动提供电子邮件,登录,全名等 - 我有错误消息,他们“不能空白”。我猜解决方案是为文本字段创建默认值以获取用户详细信息。 所以,我的问题是如何为视图中的变量提供等于谷歌帐户变量的值?
新用户注册中form_for中的电子邮件字段:
= f.email_field :email, :autofocus => true, :value => 'how can i put auth.info.email here?'
omniauth_callbacks_controller.rb:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash.notice = "Signed in Through Google!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
flash.notice = "You are almost Done! Please provide a password to finish setting up your account"
redirect_to new_user_registration_url
end
end
end
来自用户模型的 def self.from_omniauth(auth)
if user = User.find_by_email(auth.info.email)
user.provider = auth.provider
user.uid = auth.uid
user
else
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.full_name = auth.info.name
user.email = auth.info.email # THIS (user.email) value i want to provide to my registration form as default value
user.birthday = auth.info.birthday
user.avatar = auth.info.image
end
end
end
答案 0 :(得分:0)
我遇到了与GitHub相同的问题,你可以看看我的用户模型
https://github.com/flower-pot/pastebin/blob/master/app/models/user.rb