parm530
4/4/2018 - 3:24 PM

Validation with Form Errors

Rails 4: Errors on forms due to AR validations

Form Errors

  • simple validation: validates_presence_of :name
  • if the object fails this validation, then object.errors will contain an array of errors that the validation will add
# Useful error methods
object.errors.clear
object.errors.size
object.errors.each {|attr, mesg| ...}
# attr: name
# msg: name can't be blank

#returns complete sentences of the attribute and it errors
object.errors.full_messages.each {|msg| ...}