Ruby on rails RubyonRails设计确认令牌为零
我有一个用户模型(由Ruby on rails RubyonRails设计确认令牌为零,ruby-on-rails,ruby,devise,devise-confirmable,Ruby On Rails,Ruby,Devise,Devise Confirmable,我有一个用户模型(由$rails g designe User创建),它被设置为使用confirmable(在模型和迁移中) 创建用户时,不会设置确认令牌(也不会发送确认电子邮件) 下面是app/models/user.rb: class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeouta
$rails g designe User
创建),它被设置为使用confirmable(在模型和迁移中)
创建用户时,不会设置确认令牌(也不会发送确认电子邮件)
下面是app/models/user.rb
:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable, :timeoutable
def password_required?
super if confirmed?
end
def password_match?
self.errors[:password] << "can't be blank" if password.blank?
self.errors[:password_confirmation] << "can't be blank" if password_confirmation.blank?
self.errors[:password_confirmation] << "does not match password" if password != password_confirmation
password == password_confirmation && !password.blank?
end
# new function to set the password without knowing the current
# password used in our confirmation controller.
def attempt_set_password(params)
p = {}
p[:password] = params[:password]
p[:password_confirmation] = params[:password_confirmation]
update_attributes(p)
end
# new function to return whether a password has been set
def has_no_password?
self.encrypted_password.blank?
end
# Devise::Models:unless_confirmed` method doesn't exist in Devise 2.0.0 anymore.
# Instead you should use `pending_any_confirmation`.
def only_if_unconfirmed
pending_any_confirmation {yield}
end
protected
def confirmation_required?
false
end
end
class用户 self.errors[:password]这是因为您正在覆盖确认\u required?
以始终返回false
看看
创建前:生成确认令牌,如果::需要确认令牌?
只有当该方法返回true时,才会生成令牌
confirmation\u required?
的默认行为是,如果记录未被确认,则返回true
def confirmation_required?
!confirmed?
end
为了补充@nbermudezs的答案,添加了此确认\u required?
方法,以防您想绕过某些用户的确认(例如带有特殊促销代码的用户或其他用户)
如果您不想出现任何异常,我建议您只需删除这些代码行或对它们进行注释,这样您就可以返回到designe_confirmable的默认行为,这是您似乎想要的行为(以及@nbermudezs给出的行为)
你能显示你的user.rb
内容吗?也有助于了解您使用的ORM(Mongoid、ActiveRecord),我添加了user.rb
的内容。我用的是ActiveRecord。谢谢你,西里尔。我完全删除了这个方法,我所有的测试仍然通过——我认为这没有必要。它的存在是为了允许未经确认的访问;然而,我通过config/initializer/develope.rb
# def confirmation_required?
# false
# end