设计使用电子邮件或手机注册用户 [英] Devise signing up user using email or mobile no
问题描述
我关注了这篇文章,以通过电子邮件或用户名实现用户注册。 https://github.com/plataformatec/devise/wiki/操作方法:-允许用户使用其用户名登录或电子邮件地址
I followed this article to implement user registration either with email or username. https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address
User.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, authentication_keys: [:login]
attr_accessor :login
def login=(login)
@login = login
end
def login
@login || self.contact_no || self.email
end
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_hash).where(["lower(contact_no) = :value OR lower(email) = :value", { :value => login.downcase }]).first
elsif conditions.has_key?(:contact_no) || conditions.has_key?(:email)
where(conditions.to_hash).first
end
end
Devise.rb
config.authentication_keys = [ :login ]
但仍然收到错误消息电子邮件不能为空。我怎样才能解决这个问题?我在哪里弄错了?
But still getting error 'email can't be blank'. How can I fix this? Where am I getting wrong?
推荐答案
您正在关注登录文章,以解决注册问题。很简单,您需要从哪里找到此验证。这是文件。
You are following sign in article to solve issue with sign up. Simple you need to find from where this validation is coming in. Here is file .
现在要解决您的问题,您需要覆盖 email_required吗?
您正在使用devise的模型中的方法。要仅在contact_no为空或您希望满足任何条件时检查电子邮件。
Now to solve your issue, you need to override email_required?
method in model where you are using devise. To check email required only when contact_no blank or whatever condition you want.
class User < ApplicationRecord
def email_required?
self.contact_no.blank? #If contact number blank then email required.
end
end
这篇关于设计使用电子邮件或手机注册用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!