Saturday 13 April 2013

Rails Validators


Validators in Rails 3:



How to build Custom validators in Rails 3?



def validate 
  errors.add_to_base "If you are attaching an image you must enter a title for it" if         
  !upload_image.blank? and image_label.blank? 
end 

Note: add_to_base is deprecated in Rails 3 and you'll get "undefined method add_to_base ".   So, instead use :

        errors[:field_name] << "Error Message"

No comments:

Post a Comment