2013-10-09 55 views
2

我有以下色器件模型(编辑为简洁起见)设计与用户名登录空间

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先导,以实现与名称或电邮登入。

这里的问题:

学生的名字> 2层的话不能登录,例如“亚当布拉沃查理”

一个名称,例如“亚当”或< = 2个字,例如“亚当·布拉沃”的名字可以签署。

学校要求学生用自己的全名,伴随空间登录。我如何让我的学生使用全名和空格进行登录?

+2

我没有挖掘代码,但使用用户名而不是全名与空间是整个网络的约定。想想Google,Facebook,Twitter,StackOverflow。遵循约定。 –

+0

这是一个很好的建议,但我试图修改旧版应用程序。由于学校的要求,学生使用全名而不是用户名。如果证明不可行,那么我会更改应用程序以遵循约定。非常感谢 –

回答

3

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

配置/初始化/ devise.rb

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

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

3

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

+0

听起来合乎逻辑,可以实施。我如何处理现有用户? –