gpt4 book ai didi

ruby-on-rails - OmniAuth/Rails - 当你没有想到它时你有一个 nil 对象

转载 作者:数据小太阳 更新时间:2023-10-29 08:41:18 27 4
gpt4 key购买 nike

我的 Rails 应用程序出现以下错误,我不知道如何调试或修复它:

NoMethodError in AuthenticationsController#create

You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]

Rails.root: /Users/phil/Sites/travlrapp.com Application Trace | Framework Trace | Full Trace

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

Controller 是这样的:

class AuthenticationsController < ApplicationController
def index
@authentications = current_user.authentications if current_user
end

def create

omniauth = request.env["omniauth.auth"]

unless omniauth
redirect_to authentications_url
flash[:notice] = "Could not authenticate via #{params['provider']}."
end

authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
flash[:notice] = "Signed in successfully."
sign_in_and_redirect(:user, authentication.user)
elsif current_user

current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => omniauth['credentials']['token'], :secret => omniauth['credentials']['secret'])
flash[:notice] = "Authentication successful."
redirect_to authentications_url
else
user = User.new
user.apply_omniauth(omniauth)
if user.save
flash[:notice] = "Signed in successfully."
sign_in_and_redirect(:user, user)
else
session[:omniauth] = omniauth.except('extra')
redirect_to new_user_registration_url
end
end
end


def destroy
@authentication = current_user.authentications.find(params[:id])
@authentication.destroy
flash[:notice] = "Successfully destroyed authentication."
redirect_to authentications_url
end
end

OmniAuth 过去工作正常,然后我把它搞砸了,试图换成支持 flickr 的 pchilton 的 fork 。我通过在 gemfile 中设置 :git => 并尝试重新安装来做到这一点,但我不确定我是否做对了。

我现在已经手动删除了所有 omniauth 和 oafoo gem 文件,并首先安装了当前稳定版 (0.1.6) 和 git 主副本,但所有错误都是一样的。

真的很茫然,我认识的人都不知道问题出在哪里。

最佳答案

omniauth 很可能是nil。当您使用 unless onmniauth 检查 nil 时,redirect_to 实际上并没有停止执行下面的 Controller 代码。

你可能想要这样的东西:

unless omniauth
redirect_to authentications_url
flash[:notice] = "Could not authenticate via #{params['provider']}."
return
end

现在,您仍然需要弄清楚为什么 omniauthnil。为此,请查看 README 以确保您正确使用了 OmniAuth。 . /auth/provider/callback 是否路由到 AuthenticationsController#create

关于ruby-on-rails - OmniAuth/Rails - 当你没有想到它时你有一个 nil 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4705495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com