我下面这个设计如何,对How To: Allow users to sign in using their username or email address并没有详细说明所有的台阶上,但是当我尝试通过registrations/new.html.erb
形式注册我得到这个错误:“电子邮件不能为空”使用用户名或电子邮件
Email can't be blank
在我的模型,我有:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :username, :email, :password, :password_confirmation, :remember_me
attr_accessor :login
attr_accessible :login
和
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
任何意见有这个问题?
======= UPDATE
我发现这里的东西在这里■[rails]How to use Devise and Rails , without EMail它是这样的:
# Email is not required
def email_required?
false
end
用在我的模型加入我能创造一个记录用户名和离开电子邮件领域的空白,但是当我尝试没有电子邮件创建第二个记录的数据库上升了一个错误:
Mysql2::Error: Duplicate entry '' for key 'index_parents_on_email':...
我应该利用这一点,从删除索引我表在数据库中,并且只验证模型中的username
?因为我真的不需要该模型的电子邮件字段。
Mr_Nizzle,你能接受更性感的方式吗? – asiniy 2015-04-18 11:57:43