0

我正在尝试使用我的 rails 4.0.0 应用程序的谷歌帐户管理用户注册。设计完美。现有用户可以使用 Google 帐户登录。但是我在使用 Google Oauth 2 注册新用户时遇到了一些困难。例如:我有 google 帐户“example@google.com”。它已在我当前的 PC 上登录。当我尝试使用此帐户注册我的应用程序时,它会生成空白注册表单。如果我不手动提供电子邮件、登录名、全名等 - 我收到错误消息,它们“不能为空白”。我想解决方案是为文本字段创建默认值以获取用户详细信息。所以,我的问题是如何为变量提供值以等于来自谷歌帐户的变量?

新用户注册中 form_for 中的电子邮件字段:

= f.email_field :email, :autofocus => true, :value => 'how can i put auth.info.email here?'

omn​​iauth_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

来自用户模型的omniauth方法:

  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
4

1 回答 1

0

我在 GitHub 上遇到了同样的问题,你可以看看我的用户模型

https://github.com/flower-pot/pastebin/blob/master/app/models/user.rb

于 2013-11-07T12:09:59.530 回答