2013-10-09 54 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

聽起來合乎邏輯,可以實施。我如何處理現有用戶? –