2

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

AuthenticationsController#create 中的 NoMethodError

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

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

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

控制器是这样的:

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

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

4

3 回答 3

2

很可能omniauth就是这样nil。当您检查 nil 时unless onmniauthredirect_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?

于 2011-01-16T13:22:45.787 回答
1

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

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

我所做的是die('AAAAA')在控制器动作的末尾放一个,然后逐渐向上移动,直到在错误发生之前触发IT,这样我就可以准确地知道错误在哪一行。

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

我不知道 rails 是否支持这种原始调试风格。如果这些 gem 的源代码在非编译代码中也会有所帮助,这样您就可以die()像这样插入所有地方。

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

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

如果你想真正进阶,还有

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

于 2011-01-17T10:23:02.830 回答
1

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

于 2011-01-26T11:21:50.410 回答