使用空格登录设计用户名

时间:2013-10-09 11:11:47

标签: ruby-on-rails devise username

我有以下设计模型(为简洁而编辑)

class Student < ActiveRecord::Base

  devise :database_authenticatable, :token_authenticatable,
         :recoverable, :rememberable, :trackable,
         :authentication_keys => [:login], :reset_password_keys => [ :login ]

  attr_accessor :login
  attr_accessible :name, :email, :password, :password_confirmation, :login

  validates :name, :presence => true
  validates :email, :presence => true
  validates_uniqueness_of    :email,     :case_sensitive => false, :allow_blank => true, :if => :email_changed?, :scope => :id
  validates_format_of :email, :with  => Devise.email_regexp, :allow_blank => true, :if => :email_changed?
  validates_presence_of   :password, :on=>:create
  validates_confirmation_of   :password, :on=>:create
  validates_length_of :password, :within => Devise.password_length, :allow_blank => true

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions).where(["name = :value OR lower(email) = :value", { :value => login.downcase }]).first
    else
      where(conditions).first
    end
  end

  def email_required?
    false
  end

  def email_changed?
    false
  end

end

我已按照this link上的指南启用以姓名或电子邮件方式登录。

问题在于:

姓名> gt的学生2个字不能登录,例如“Adam Bravo Charlie”

可以登录单个名称,例如“Adam”或带有&lt; = 2个单词的名称,例如“Adam Bravo”。

学校要求学生使用他们的全名和随行空间登录。如何让我的学生使用他们的全名和空格登录?

2 个答案:

答案 0 :(得分:3)

在登录表单上,我会接受一个字符串并将其转换为slug。使用slug作为username进行身份验证。创建新用户时,请使用before_create方法和parameterize全名。这将允许您拥有无限数量的空格,包括标点字符(句点和逗号)。这会让用户感觉用户的用户名输入他们的用户名,反过来,它会parameterize用户的全名,并将其用作登录的用户名。

答案 1 :(得分:3)

好的结果是解决方案比我想象的要简单。

config / initializers / devise.rb

config.case_insensitive_keys = [ :name]
config.strip_whitespace_keys = [ :name ]

瞧,现在可以使用全名登录!