Lovish Choudhary
Lovish Choudhary

Reputation: 167

NoMethodError(undefined method `persisted?' for true:TrueClass)

I am implementing a rails webapp, in this app I am using Devise+ Omniauth for authentication. In my Devise model I am also using confirmable block.

Now when I try to implement social login through omniauth for facebook or google I am getting the following error

  NoMethodError at /customer/auth/google_oauth2/callback
  undefined method `persisted?' for true:TrueClass

Here is the code for my omniauth_callbacks controller

    class OmniauthCallbacksController < Devise::OmniauthCallbacksController

    skip_before_filter :authenticate_user!
    def all
        p env["omniauth.auth"]
        user = User.from_omniauth(env["omniauth.auth"], current_user)
        if user.persisted?
            flash[:notice] = "You are in..!!! Go to edit profile to see the status for the accounts"
            sign_in_and_redirect(user)
        else
            session["devise.user_attributes"] = user.attributes
            redirect_to new_user_registration_url
        end
    end

      def failure      
        super
      end


    alias_method :facebook, :all
    alias_method :google_oauth2, :all
end

This is the code snippet from my user model

class User < ActiveRecord::Base

devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable,:confirmable,:token_authenticatable

def self.from_omniauth(auth, current_user)    
    authorization = Authorization.where(:provider => auth.provider, 
                    :uid => auth.uid.to_s, 
                    :token => auth.credentials.token, 
                    :secret => auth.credentials.secret).first_or_initialize

    if authorization.user.blank?
        user = current_user || User.where('email = ?', auth["info"]["email"]).first        
        if user.blank?       
          user = User.new
          user.name = auth.info.name
          user.email= auth.info.email
          if auth.provider == "twitter" 
            user.save(:validate => false) 
          else
            user.skip_confirmation!
            user.save(:validate => false)
          end
        end
        authorization.username = auth.info.nickname
        authorization.user_id = user.id
        authorization.save
    end
    authorization.user
end

end

My user model has many authorization model (created to handle accounts from multiple providers)

class Authorization < ActiveRecord::Base
    belongs_to :user

    after_create :fetch_details



    def fetch_details
        self.send("fetch_details_from_#{self.provider.downcase}")
    end


    def fetch_details_from_facebook
        graph = Koala::Facebook::API.new(self.token)
        facebook_data = graph.get_object("me")
        self.username = facebook_data['username']
        self.save
    end
 end

Can anyone help me in finding why I am getting this undefined method `persisted?' for true:TrueClass

error?

Upvotes: 0

Views: 1521

Answers (1)

miler350
miler350

Reputation: 1421

What role is the current_user helper doing in the omniauth line? I'm assuming it is creating some sort of conflict with the omniauth environment variable.

Try changing:

user = User.from_omniauth(env["omniauth.auth"], current_user)

to:

user = User.from_omniauth(env["omniauth.auth"])

Upvotes: 0

Related Questions