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 によるフォークにスワップしようとして、OmniAuth をマッシュアップしました。これを行うには、gemfile で :git => を設定して再インストールを試みましたが、正しく行ったことがあるとは確信していません。

すべての omniauth および oa-foo gem ファイルを手動で削除し、最初に現在の安定版 (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

ここで、 が である理由を理解する必要がありomniauthますnilそのためには、 READMEを見て、OmniAuth を正しく使用していることを確認してください。/auth/provider/callbackにルーティングされAuthenticationsController#createますか?

于 2011-01-16T13:22:45.787 に答える
1

これはランダムに修正されたようです。レールに乗れ!

于 2011-01-26T11:21:50.410 に答える
1

もしあなたがこの方法を既に知っていたら、あらかじめお詫びします (結局、あなたは PHP 開発者です)。

Rails は に似た PHP スタイルのデバッグをサポートしていdie()ますか? yii と kohana php フレームワークの両方で、このような奇妙な不可解なエラーに遭遇しました。

私がしていることは、コントローラのアクションの最後に a を置き、エラーが発生する前にITdie('AAAAA')がトリガーされるまで徐々に上に移動することです。これにより、エラーが発生した行を正確に知ることができます。

次に、その行で呼び出された関数に移動して、最初からやり直します。

Rails がこの種の raw デバッグ スタイルをサポートしているかどうかはわかりません。また、これらの gem のソース コードがコンパイルされていないコードであるとdie()、そのような場所全体に挿入できるようになると役立ちます。

同等echo 'AAA'; exit;または類似の何かを行うことができます。

また、「関数が呼び出されたかどうかを確認する: die('BBBBB');:P

あなたが本当に高度に行きたいなら、それもあります

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

于 2011-01-17T10:23:02.830 に答える