使用用户名而不是电子邮件进行身份验证

nul*_*tek 18 ruby-on-rails devise

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

我已经在users表中添加了一个字符串列"username",将字段从:email更改为:登录表单中的:username,并将devise.rb中的身份验证密钥更改为:username但是当我转到登录我遇到了这个提示:"请输入一个电子邮件地址".

我究竟做错了什么?

**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 ]
Run Code Online (Sandbox Code Playgroud)

rb5*_*512 43

在你的config /初始化/ devise.rb取消注释config.authentication_keys = [ :email],并更改为config.authentication_keys = [ :username ]

更新:
您的表单不正确.
更改f.email_fieldf.text_field