Christopher Warrington
Christopher Warrington

Reputation: 767

undefined local variable or method `password'

Error Message

undefined local variable or method `password' for #<Class:0x007f978068b9f0>

Extracted source (around line #52):

end

    def password.required?
        super && provider.blank?
    end

Explanation

I am following this tutorial on how to implement OmniAuth and copied his code exactly... I think. I have reached out to the the video owner and haven't heard back, and found this tutorial on his website and it looks like it was actually made in 2012, not 2015 like the YouTube video publish date shows. So, it may be possible that the code I am using is not compatible with the versions of Ruby, Rails or Devise that I have installed. (One of the reasons I was looking for a tutorial from this past year for implementing OmniAuth with Devise).

Question

Why is the error stating it is an undefined local variable when it is being defined at that line 52? It isn't used anywhere else in that file (User Model), but is elsewhere (login page and profile update page).

Versions Used

Ruby: ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]

Rails: Rails 4.2.0

Devise: 3.4.1

Code

The User Model:

## app/models/user.rb

01  class User < ActiveRecord::Base
02    
03      devise  :database_authenticatable, :registerable, :omniauthable,
04              :recoverable, :rememberable, :trackable, :validatable
05    
06      validates :birthday, :presence => true
07      validates :user_name, :presence => true, :uniqueness => { :case_sensitive => false }
08    
09      ################################################
10      ## Start section for User Name or Email Login ##
11      ################################################
12    
13      attr_accessor :login
14    
15      def self.find_for_database_authentication(warden_conditions)
16          conditions = warden_conditions.dup
17          if login = conditions.delete(:login)
18              where(conditions.to_h).where(["lower(user_name) = :value OR lower(email) = :value", { :value => login.downcase }]).first
19          else
20              where(conditions.to_h).first
21          end
22      end
23    
24      ##############################################
25      ## End section for User Name or Email Login ##
26      ##############################################
27    
28    
29      ##############################################
30      ## Start section for OmniAuth Authorization ##
31      ##############################################
32    
33      def self.from_omniauth(auth)
34          where(auth.slice(:provider, :uid)).first_or_create do |user|
35              user.provider = auth.provider
36              user.uid = auth.uid
37              user.user_name = auth.info.nickname
38          end
39      end
40    
41      def self.new_with_session(params, session)
42          if session["devise.user_attributes"]
43              new(session["devise.user_attributes"], without_protection: true) do |user|
44                  user.attributes = params
45                  user.valid?
46              end
47          else
48              super
49          end
50      end
51    
52      def password.required?
53          super && provider.blank?
54      end
55    
56      def update_with_password(params, *options)
57          if encrypted_password.blank?
58              update_attributes(params, *options)
59          else
60              super
61          end
62      end
63    
64      ############################################
65      ## End section for OmniAuth Authorization ##
66      ############################################
67  end

The OmniAuth Callbacks Controller (I don't think this is part of the problem, but in case it is, or someone else needs the code):

## app/controllers/omniauth_callbacks_controller.rb

01  class OmniauthCallbacksController < Devise::OmniauthCallbacksController
02      
03      def all
04          user = User.from_omniauth(request.env["omniauth.auth"])
05          if user.persisted?
06              flash.notice = "You have been logged in."
07              sign_in_and_redirect user, :event => :authentication
08           else
09              session["devise.user_attributes"] = user.attributes
10               redirect_to new_user_registration_url
11           end
12      end
13    
14      alias_method :facebook, :all
15      alias_method :google, :all
16      alias_method :twitter, :all
17      alias_method :amazon, :all
18      alias_method :github, :all
19  end

Thank you in advance for your help.

Upvotes: 0

Views: 1188

Answers (1)

Soufyane Bouchaala
Soufyane Bouchaala

Reputation: 227

You have a typo on line #52:

You should use:

def password_required?

instead of:

def password.required?

Upvotes: 4

Related Questions