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

在 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 => "長度不正確" }

相关推荐