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

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

问题描述

我是 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.

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

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 中取消注释 config.authentication_keys = [ :email] 并将其更改为 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

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

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