删除设计电子邮件验证

时间:2016-09-27 13:09:47

标签: ruby-on-rails devise ruby-on-rails-5

我使用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}]}>

为什么这样? 感谢

3 个答案:

答案 0 :(得分:0)

您的验证不会覆盖设计验证,只是扩展它们。如果您确实要删除Devise验证,可以删除:validatable模块。但是你也会丢失所有漂亮的验证(例如密码)。

或者首先尝试这样做:(我没有测试过这个) :authentication_keys => {email: false, login: true}

答案 1 :(得分:0)

如果您只想删除电子邮件验证。将用户模型编辑为

   devise :database_authenticatable, :registerable,:recoverable, :rememberable,
   :trackable, :validatable, authentication_keys: [:password]

如果你完全想要删除验证。从用户模型中删除 validatable

答案 2 :(得分:0)

在我的情况下,我更改为用户名字段。首先我更改了/config/initializers/devise.rb - &gt; config.authentication_keys = [: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
end