2011-03-17 50 views
1

我想用設計我的身份驗證和而不必電子郵件作爲登錄我希望用戶在用戶自己的用戶名使用USER_NAME的登錄界面,在色器件

我已經加入USER_NAME到用戶表 添加到用戶模型attr_accessible:電子郵件,:密碼,:password_confirmation,:remember_me,:USER_NAME

並且還config.authentication_keys = [:USER_NAME]在 '/intilizers/devise.rb'

但我的問題是我怎麼能跳過電子郵件驗證,並USER_NAME代替

我使用Rails3中和裝置提前1.1.8

感謝

歡呼

sameera

回答

3

設計沒有預見到根本沒有電子郵件字段的選項,因此必須在Rails級別完成。

遺憾的是Rails不提供一種方法如何刪除驗證,但是Rails可以進行擴展,這已被媒體鏈接在this plugin做,只是來看看active_record extension,這是很簡單的。

基本上你添加驗證去除功能active_record

module ActiveRecord 
    class Base 
    def self.clear_validations 
     @validate_callbacks = [] 
    end 
    def self.remove_validation(sym) 
    @validate_callbacks.reject! {|validation| validation.options[:name] == sym} 
    end 
    def self.remove_validation_group(sym) 
    @validate_callbacks.reject! {|validation| validation.options[:group] == sym} 
    end 
    end 
end 

和擴展色器件的用戶模型來刪除電子郵件驗證

require 'active_record_validation_extender' 

module UserValidationExtender 
    def self.included(base) 
    base.class_eval do 
     base.remove_validation(:email) 
    end  
    end 
end 
+0

嗨瓦爾迪,雖然我還沒有嘗試過你的榜樣,你的概念是真的很酷。非常感謝 – sameera207 2011-03-17 20:34:07

17

在你可以只添加email_required最近制定的版本?方法到您的用戶模型。 它關閉驗證模塊中的電子郵件驗證。

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    def email_required? 
    false 
    end 
end 
0

Devise已經提供瞭如何設置的指導。

如果你想使用用戶名(或其他)而不是電子郵件看看這個。 (請記住,您仍然需要在註冊頁面上提供電子郵件) https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-with-something-other-than-their-email-address

如果您想使用電子郵件或用戶名,請查看此信息。

https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address