我正在使用此代码块来验证电子邮件地址。输入的电子邮件地址的格式验证良好,但问题在于"唯一性" part - 我目前可以在数据库中输入更多符合标准的电子邮件地址 - 这怎么可能?
Rails 4中有关于验证的内容有所改变吗?
class BetaAccess < ActiveRecord::Base
validates_format_of :email,:with => Devise::email_regexp, uniqueness: true
end
谢谢。
答案 0 :(得分:4)
试试这个:
class BetaAccess < ActiveRecord::Base
validates :email,format: {with: Devise::email_regexp}, uniqueness: true
end
答案 1 :(得分:1)
format
和uniqueness
是不同的验证器,如果您想在一行中使用,则应使用validates
方法。
validates :email, :format => { :with => Devise::email_regexp }, :uniqueness => true
答案 2 :(得分:0)
validates_format_of :email,:with => Devise::email_regexp, uniqueness: true
将唯一性结合到格式验证中。使用validates
语法
validates :email,format: {with: Devise::email_regexp},
uniqueness: true
另外,使用新的ruby语法进行哈希处理。那种整洁的方式