未定义的局部变量或方法“密码”
undefined local variable or method `password'
错误信息
undefined local variable or method `password' for #<Class:0x007f978068b9f0>
Extracted source (around line #52):
end
def password.required?
super && provider.blank?
end
说明
我正在关注 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,看起来它实际上是在 2012 年制作的,而不是 YouTube 视频发布日期显示的 2015 年。因此,我使用的代码可能与我安装的 Ruby、Rails 或 Devise 的版本不兼容。 (我一直在寻找去年使用 Devise 实施 OmniAuth 的教程的原因之一)。
问题
为什么在第 52 行定义它时,错误说明它是一个未定义的局部变量?它没有在该文件的其他任何地方使用(用户模型),而是在其他地方使用(登录页面和配置文件更新页面)。
使用的版本
Ruby: ruby 2.2.1p85(2015-02-26 修订版 49769)[x86_64-darwin14]
Rails: Rails 4.2.0
设计:3.4.1
代码
用户模型:
## 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(我不认为这是问题的一部分,但以防万一,或者其他人需要代码):
## 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
预先感谢您的帮助。
第 52 行有错别字:
你应该使用:
def password_required?
而不是:
def password.required?
错误信息
undefined local variable or method `password' for #<Class:0x007f978068b9f0>
Extracted source (around line #52):
end
def password.required?
super && provider.blank?
end
说明
我正在关注 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,看起来它实际上是在 2012 年制作的,而不是 YouTube 视频发布日期显示的 2015 年。因此,我使用的代码可能与我安装的 Ruby、Rails 或 Devise 的版本不兼容。 (我一直在寻找去年使用 Devise 实施 OmniAuth 的教程的原因之一)。
问题
为什么在第 52 行定义它时,错误说明它是一个未定义的局部变量?它没有在该文件的其他任何地方使用(用户模型),而是在其他地方使用(登录页面和配置文件更新页面)。
使用的版本
Ruby: ruby 2.2.1p85(2015-02-26 修订版 49769)[x86_64-darwin14]
Rails: Rails 4.2.0
设计:3.4.1
代码
用户模型:
## 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(我不认为这是问题的一部分,但以防万一,或者其他人需要代码):
## 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
预先感谢您的帮助。
第 52 行有错别字:
你应该使用:
def password_required?
而不是:
def password.required?