2016-09-27 87 views
0

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

为什么呢? 谢谢

+0

请试试这个。将'email_required?'方法移动到'user.rb'中 –

+0

'email_required?'已在'user.rb'中 – motoroller

+0

从'user.rb'移除'protected' –

回答

0

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

或者,也许尝试做到这一点首先:(我没有测试这个) :authentication_keys => {email: false, login: true}

+0

谢谢,但它也做了没有帮助。顺便说一下,我决定删除validatable – motoroller

+0

。在控制台中:'puts'语句阻止执行其余的代码。你最好在'false'之后输入'byebug'并在控制台中看到输出值是什么。 – Mauddev

+0

它适用于我https://github.com/motoroller95/iMed/blob/master/app/models/user.rb#L7 – motoroller

0

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

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

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

0

在我的情况下,我更改为用户名字段。首先我更改了/config/initializers/devise.rb - > config.authentication_keys = [:用户名],并且,因为我使用的是旧数据库,所以我必须将主键更改为用户名,并更改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