Rails 3+之后支持的validate整合写法

忧郁深蓝 2011-10-19

在 Rails3 之後支援以下的整合寫法:

validates :name,  :presence => true, 
                  :length => {:minimum => 1, :maximum => 254}
               
validates :email, :presence => true, 
                  :length => {:minimum => 3, :maximum => 254},
                  :uniqueness => true,
                  :email => true

如果需要客製化錯誤訊息的話:

validates :name,  :presence => { :message => "不能空白" } , 
                  :length => {:minimum => 1, :maximum => 254, :message => "長度不正確" }

相关推荐

AllenYoung / 0评论 2014-07-04
jizhename / 0评论 2013-09-03