Devise使用用户名而不是电子邮件进行身份验证 [英] Devise authenticating with username instead of email

查看:276
本文介绍了Devise使用用户名而不是电子邮件进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Devise的新人,通过使用电子邮件地址作为身份验证密钥,可以正常工作。但是,我有一个用例需要一个用户名,而我似乎无法使其工作。

I'm new to Devise and have it working fine by using an email address as the authentication key. However, I have a use case which requires a username instead and I can't seem to get it working.

我已经添加了一个字符串列用户名用户表,将字段从:电子邮件更改为登录表单中的用户名,并将devise.rb中的身份验证密钥更改为:用户名,当我登录时遇到此提示:请输入电子邮件地址。

I've added a string column, "username" to the users table, changed the fields from :email to :username in the sign-in form, and have changed the authentication key in devise.rb to :username yet when I go to sign in I'm met with this prompt: "Please enter an email address".

我做错了什么?

**new.html.erb**

  <div><%= f.label :username %><br />
  <%= f.email_field :username %></div>

**User.rb**
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:username]

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username
  # attr_accessible :title, :body
end

**devise.rb**
 config.authentication_keys = [ :username ]


推荐答案

在你的config / initializers / devise.rb uncomment config.authentication_keys = [:email] code>并将其更改为 config.authentication_keys = [:username]

In your config/initializers/devise.rb uncomment config.authentication_keys = [ :email] and change it to config.authentication_keys = [ :username ]

更新:

您的表单不正确。

f.email_field 更改为 f。 text_field

这篇关于Devise使用用户名而不是电子邮件进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆