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

とUserモデルを持っていますが、電子メールの検証を考案することは、まだ

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.rebired?'メソッドを 'user.rb'に移動 –

+0

' email_required? 'は既に' user.rb'にあります – motoroller

+0

'user.rb'から' protected'を削除します –

答えて

0

あなたの検証はDeviseの検証よりも優先されません。本当にDevise検証を削除したい場合は、検証可能モジュールを削除することができます。しかし、(パスワードなどの)きちんとした検証もすべて失うことになります。

それとも、最初にこれをやろう(私はこれをテストしていない) :authentication_keys => {email: false, login: true}

+0

ありがとうございました助けません。私は有効なものを削除することを決めた – motoroller

+0

ところで。 console:'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] 

に編集してください。完全にバリデーションを削除したい場合は、ユーザーモデルから有効性を確認してを削除します。

0

私の場合、ユーザー名フィールドに変更されました。最初に私は/config/initializers/devise.rb-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 
関連する問題