打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
rails中validates及各种方法
 

rails中validates及各种方法

 2157人阅读 评论(0) 收藏 举报
 分类:
 

首先是不为空validates :name,presence:{message:'blank is not allowed'}

唯一性:validates :name,uniqueness:{message:'already exist!',case_sensitive: false}#case_sensitive区分大小写

长度:validates :name,length:{in: 6..20,message:'must in 6-20 byte'} 

格式:validates :name,format:{with: /[a-z]/,message:'must contain at least one lower case character'} 

范围:validates :country,inclusion:{in:['China','USA','Jpan','Mexico'],message:'must one of China USA Jpan Mexico '} 

           validates_inclusion_of :done_ratio, :in => 0..100

数字:validates :graduate,numericality:{allow_nil: true,greater_than: 1920,less_than_or_equal_to:Time.now.year}


 

 validates :description,presence:true,if: :require_description_presence?

 def require_description_presence?

      self.can_send_mail

 end 

 这个validates :description,presence: true只有在require_description_presence?方法返回true的时候才会起作用

 

validate :description_length_words

 def description_length_words

    unless self.description.blank? then

       num_words = self.description.split.length

       if num_words < 5 then

         self.errors.add(:description,'must be at least 5 words long')

       elsif num_words > 50 

          self.errors.add(:description,'must be at most 50 words long')

        end 

    end 

end

 

validates_exclusion_of 是和validates_inclusion_of相反的!
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
DL之CNN:利用CNN(keras, CTC loss, {image_ocr})算法实现OCR光学字符识别
[Python]怎样使用表单 forms
串联所有单词的子串
r语言文本分析主题模型文本分类文本聚类
在PHP中操作文件的扩展属性
Create win32 services using Python and py2exe...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服