新手问题 Devise 中 email 的 presence 和 unique 验证是在哪里?

dragonszy · 2013年05月26日 · 3253 次阅读

新手,刚刚开始学 Rails,各种问题不得其解 Question:

  1. devise 中 email 的 presence 和 unique 验证是写在哪里的?
  2. devise 中增加了 username,存在性和唯一性验证必须写在 user.rb 中吗?(能不能通过什么 devise 的设置实现,想把用户注册的验证放一起,username 和 email 一起)
  3. 验证 username 不能出现指定的单词或中文,应该怎么做?比如不能出现 fuxk 之类的,请问哪里有相关资料?
  4. 如果用:in => 3..20 的话,无论中文、英文都是 3 个字符,如何区分对待 (中文 3 个,英文 6 个)?

现在 user.rb 如下,能实现注册登录验证

class User < ActiveRecord::Base
  rolify
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :invitable, :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]

  attr_accessor :login
  # Setup accessible (or protected) attributes for your model
  attr_accessible :role_ids, :as => :admin
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me
  attr_accessible :login

  validates :name, :presence => true, :length => { :in => 3..20 }, :uniqueness => true

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

end
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号