【问题标题】:NoMethodError (undefined method `info' for nil:NilClass) `find_for_google_oauth2'NoMethodError(nil:NilClass 的未定义方法“信息”)“find_for_google_oauth2”
【发布时间】:2017-10-03 05:41:33
【问题描述】:

!我正在尝试允许同事和学生使用他们的谷歌项目帐户进行注册。我已经得到了传递的令牌,但是我得到了一个错误。

  1. app/models/user.rb:70:in `find_for_google_oauth2'

app/controllers/authentications_controller.rb:5:in `create'

我的认证控制器创建方法

    def create
       auth = request.env[":google_oauth2"]
       @user = User.find_for_google_oauth2(request.env["omniauth.auth"])
       redirect_to root_url, :notice => "Signed in!"
   end

我的用户模型

def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
       user.email = auth.info.email
       user.password = Devise.friendly_token[0,20]
       user.first_name = auth.info.first_name
       user.last_name = auth.info.last_name   # assuming the user model has a name
       user.image = auth.info.image # assuming the user model has an image
  end
 end

    def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
        data = access_token.info
        user = User.where(:email => data["email"]).first


    unless user
        user = User.create(name: data["name"],
             email: data["email"],
             uid: access_token.uid,
             provider: access_token.provider,
             password: Devise.friendly_token[0,20]
            )
          end
          user
        end

还有我的 config/omniauth.rd

require 'omniauth-google-oauth2'

OmniAuth.config.logger = Rails.logger
  OmniAuth.logger.progname = "omniauth"
Rails.application.config.middleware.use OmniAuth::Builder do



    provider :google_oauth2, '***********petnk.apps.googleusercontent.com',
        google_client_secret: '***********',
        prompt: "consent",
        select_account: true,
        scope: 'userinfo.email',
        image_aspect_ratio: 'square',
        image_size: 50
        on_failure { |env|AuthenticationsController.action(:failure).call(env) }

    end

不知道要包括什么来帮助你们帮助我。我是 Rails 新手,过去一年一直在努力学习,因此感谢您的帮助。

谢谢

【问题讨论】:

  • 有什么方法可以检查 self. find_for_google_oauth2 中的 access_token 是否为 nil?
  • @SebastiánPalma 好问题,不太确定我会怎么做?我现在有点语法问题,但我似乎找不到它,我到处找。
  • 不知道为什么我在没有解释的情况下被否决了,我希望这是一个可以帮助和教导的社区,而不是羞耻和告诫。
  • 每次投反对票时都留言会很好,除非原因很明显,但我认为情况并非如此。在方法定义之后放置 byebug 以检查值,例如 def self.find_for_google_oauth2(access_token, signed_in_resource=nil); byebug,这将“停止”应用程序,您将能够在同一工作流程中但在 Rails 服务器中检查这些变量。
  • @SebastiánPalma 很好的建议,这就是结果。 AuthenticationsController#create 作为 HTML 参数处理:{“google_oauth2”=>“google_oauth2”} [65, 74] ...(为简洁起见,我将其编辑了)66:结束 67:结束 68:69:def self.find_for_google_oauth2( access_token,已签名的_in_resource=nil); byebug => 70: data = access_token.info 71: user = User.where(:email => data["email"]).first 72: 73: unless user 74: user = User.create(name: data[" name"], (byebug)

标签: ruby-on-rails-5 omniauth google-authentication nomethoderror


【解决方案1】:

我不得不将我的创建功能从我的身份验证控制器移动到我的全能控制器。不太清楚为什么这解决了问题,但现在确实解决了。

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 2013-01-14
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多