OmniAuth / Rails – 当你没想到它时,你有一个零对象

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

AuthenticationsController中的NoMethodError #create

当你没想到它时,你有一个零对象! 您可能期望ActiveRecord :: Base的实例。 在评估nil时发生错误。[]

Rails.root:/Users/phil/Sites/travlrapp.com应用程序跟踪| 框架跟踪| 完整跟踪

app / controllers / authentications_controller.rb:15:在’create’中

控制器是这样的:

class AuthenticationsController  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和oa-foo gem文件,并首先安装了当前的stable(0.1.6)和git master副本,但所有错误都是相同的。

真的在这里不知所措,我认识的任何人都不知道问题是什么。

omniauth很可能是nilunless onmniauth检查nil, unless onmniauth redirect_to实际上不会停止执行下面的控制器代码。

你可能想要这样的东西:

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

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

如果你已经知道这个方法(毕竟你是一个php开发人员),我会提前道歉。

rails是否支持类似于die() php样式调试? 我在yii和kohana php框架中都遇到过类似奇怪的难以理解的错误。

我所做的是在控制器acion的末尾加一个die('AAAAA') ,并逐渐向上移动,直到IT在错误发生之前被触发,这样我就知道错误在哪一行。

然后我将它移动到该行上调用的任何函数并重新开始。

我不知道rails是否支持这种原始调试风格。 如果这些gem的源代码在非编译代码中,那么你可以在这样的地方插入die()

你可以做类似echo 'AAA'; exit;事情echo 'AAA'; exit; echo 'AAA'; exit; 或类似的东西。

还有’检查函数是否被调用: die('BBBBB'); :P

如果你想要真正先进,也有

die("AAAAA ".' '.__FILE__.'::Line:'.__LINE__);

这似乎是随机修复的。 去铁路吧!