我使用devise和devise-token-auth。我有以下内容的用户模型
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]
include DeviseTokenAuth::Concerns::User
validates_uniqueness_of :login
validates_presence_of :full_name
protected
def email_required?
puts "email_required called"
false
end
end但设计式电子邮件验证仍在运行
2.3.0 :003 > user = User.new(login: "hello", password: "11111111", password_confirmation: "11111111", full_name: "hello world")
=> #<User id: nil, provider: "email", uid: "", email: nil, full_name: "hello world", login: "hello", created_at: nil, updated_at: nil>
2.3.0 :004 > user.valid?
email_required? called #<===== my method is called!
(3.7ms) SELECT COUNT(*) FROM "users" WHERE "users"."provider" = $1 AND "users"."email" IS NULL [["provider", "email"]]
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."login" = $1 LIMIT $2 [["login", "hello"], ["LIMIT", 1]]
=> false
2.3.0 :005 > user.errors
=> #<ActiveModel::Errors:0x000000028e2338 @messages={:email=>[...]}, @details={:email=>[{:error=>:blank}]}>为什么是这个?谢谢
发布于 2016-09-28 07:41:24
您的验证不会覆盖设计验证,它们只是扩展它们。如果您真的想删除设备验证,可以删除:validatable模块。但是你也会丢失所有有用的验证(例如密码)。
或者可以先试着这样做:(我没有测试这个) :authentication_keys => {email: false, login: true}
发布于 2016-09-28 17:53:24
如果您只想删除电子邮件验证。编辑用户模型以
devise :database_authenticatable, :registerable,:recoverable, :rememberable,
:trackable, :validatable, authentication_keys: [:password]如果您确实想要删除验证。从用户模型中删除可验证。
发布于 2016-09-28 22:34:42
在我的例子中,我更改为用户名字段。首先,我更改了/config/initializers/devise.rb --> config.authentication_keys = [:username]
而且,由于我使用的是遗留数据库,我还必须将主键更改为username,并更改auth方法
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:username)
where(["APP_NAME = '#{$app_name.downcase}' AND USERNAME = :value", { :value => login }]).first
endhttps://stackoverflow.com/questions/39725552
复制相似问题