OmniAuth / Rails - You have a nil object when you didn't expect it

北城余情 提交于 2019-12-10 10:32:25

问题


I'm getting the following error in my Rails application and I have no idea how to go about debugging or fixing it:

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'

The controller is this:

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 used to work fine, then I mashed it up trying to swap to a fork by pchilton which supported flickr. I did this by setting :git => in the gemfile and trying to reinstall but im not confident I ever did it right.

I have now manually removed all omniauth and oa- foo gem files and installed first the current stable (0.1.6) and the git master copy but all errors are the same.

Really at a loss here, nobody I know has any idea what the problem is.


回答1:


It's probable that omniauth is nil. While you are checking for nil with unless onmniauth, the redirect_to doesn't actually stop the controller code below from executing.

You probably want something like this:

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

Now, you still need to figure out why omniauth is nil. For that, make sure you are using OmniAuth correctly by looking at the README. Is /auth/provider/callback routed to AuthenticationsController#create ?




回答2:


I apologize in advance if you already know this method (you are a php developer after all).

Does rails support php style debugging similar to die() ? I have encountered weird incomprehensible error like this in both yii and kohana php frameworks.

What I do is put a die('AAAAA') at the end of the controller acion, and gradually move it up until IT gets triggered before the error does, that way I know exactly on what line the error was.

Then i move it into whatever function is called on that line and start again.

I don't know if rails supports this kind of raw debug style. Also it would help if the source code for those gems are in noncompiled code so you can insert die() all over the place like that.

You could do something like the equivalent of echo 'AAA'; exit; or something similar.

Also there is also the 'check if a function gets called: die('BBBBB'); :P

If you want to go really advanced there is also

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




回答3:


This seemed to randomly fix itself. Go Rails!



来源:https://stackoverflow.com/questions/4705495/omniauth-rails-you-have-a-nil-object-when-you-didnt-expect-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!