开发者

using user_name for login in devise

I want to use devise for my authentication and instead of having e-mail as the login i want users to user their user names

for that

I have added user_name to the users table added to the User model attr_accessible :email, :开发者_开发问答password, :password_confirmation, :remember_me, :user_name

and also config.authentication_keys = [ :user_name ] in '/intilizers/devise.rb'

But my question is how can i skip the e-mail validation and have user_name instead

I'm using rails3 and device 1.1.8

thanks in advance

cheers

sameera


In recent Devise versions you can just add email_required? method to your user model. It turns off e-mail validation in validatable module.

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

  def email_required?
    false
  end
end


Devise has not foreseen an option to not have a email field at all, therefore this has to be done on Rails level.

Unfortunately Rails does not offer a way how to remove validations, however rails can be extended and this has allready been done in this plugin, just take a look at the active_record extension, it is quite straight forward.

Essentially you add validation removal functions to 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 

and extend the user model of devise to remove the email validation

require 'active_record_validation_extender'

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


Devise has provided instructions for how to set that up.

If you want to use Usernames(or whatever) instead of emails take a look at this. (just remember, you will still need to require an email at the sign up page) https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-with-something-other-than-their-email-address

If you want to use emails or a username take a look at this.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜