2016-09-27 106 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