ruby in rails 在设计中确认电子邮件时出错
ruby in rails Error while confirming email in devise
我正在使用 devise on rails5 来确认用于登录的电子邮件地址。一切正常,直到我单击确认 link。然后它告诉我这个错误:
NameError in Devise::ConfirmationsController#show
undefined local variable or method `signin' for #< Class:0x007fb1cbe56b48>
这是导致错误的代码:
conditions = warden_conditions.dup
where(conditions).where(["lower(username) = :value OR lower(email)
= :value", { :value => signin.downcase }]).first
end
以上代码属于我的模型:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
attr_accessor :signin
validates :username, :uniqueness => {:case_sensitive => false}
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
where(conditions).where(["lower(username) = :value OR lower(email)
= :value", { :value => signin.downcase }]).first
end
有人可以帮我解决一下吗?如果你给我一些关于这个方法如何工作的信息?
我 post 我在这里回答你的评论。我不知道你是如何处理登录的,但我是在会话控制器和会话助手中进行登录的:
#Session Controller
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:email].downcase)
log_in user
redirect_back_or user
flash[:success] = "You are logged in"
else
flash[:danger] = "Wrong email or password"
render 'new'
end
end
我使用的"log_in(user)"方法是在Session Helper中定义的。
#Session Helper
def log_in(user)
session[:user_id] = user.id
end
首先,attr_accessor
定义了一个实例方法,而不是 class 方法,所以如果你在 class 方法中调用 signin
,比如 self.find_first_by_auth_conditions
会抛出 undefined local variable or method 'signin' for #< Class:0x007fb1cbe56b48>
错误。
其次,您的find_for_database_authentication
方法不完整。你应该根据 this example:
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_h).where([
"lower(username) = :value OR lower(email) = :value",
{ :value => login.downcase }
]).first
elsif conditions.has_key?(:username) || conditions.has_key?(:email)
where(conditions.to_h).first
end
end
我不确定您是否已经使用该示例作为基础,但如果您解释修改它的原因是什么,那就太好了。我没有理由,就用那段代码。
我正在使用 devise on rails5 来确认用于登录的电子邮件地址。一切正常,直到我单击确认 link。然后它告诉我这个错误:
NameError in Devise::ConfirmationsController#show
undefined local variable or method `signin' for #< Class:0x007fb1cbe56b48>
这是导致错误的代码:
conditions = warden_conditions.dup
where(conditions).where(["lower(username) = :value OR lower(email)
= :value", { :value => signin.downcase }]).first
end
以上代码属于我的模型:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
attr_accessor :signin
validates :username, :uniqueness => {:case_sensitive => false}
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
where(conditions).where(["lower(username) = :value OR lower(email)
= :value", { :value => signin.downcase }]).first
end
有人可以帮我解决一下吗?如果你给我一些关于这个方法如何工作的信息?
我 post 我在这里回答你的评论。我不知道你是如何处理登录的,但我是在会话控制器和会话助手中进行登录的:
#Session Controller
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:email].downcase)
log_in user
redirect_back_or user
flash[:success] = "You are logged in"
else
flash[:danger] = "Wrong email or password"
render 'new'
end
end
我使用的"log_in(user)"方法是在Session Helper中定义的。
#Session Helper
def log_in(user)
session[:user_id] = user.id
end
首先,attr_accessor
定义了一个实例方法,而不是 class 方法,所以如果你在 class 方法中调用 signin
,比如 self.find_first_by_auth_conditions
会抛出 undefined local variable or method 'signin' for #< Class:0x007fb1cbe56b48>
错误。
其次,您的find_for_database_authentication
方法不完整。你应该根据 this example:
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_h).where([
"lower(username) = :value OR lower(email) = :value",
{ :value => login.downcase }
]).first
elsif conditions.has_key?(:username) || conditions.has_key?(:email)
where(conditions.to_h).first
end
end
我不确定您是否已经使用该示例作为基础,但如果您解释修改它的原因是什么,那就太好了。我没有理由,就用那段代码。