I
I
IvanN7772014-10-19 11:51:11
Ruby on Rails
IvanN777, 2014-10-19 11:51:11

How to get error data when validating fields in a Rails model?

I'm not saving in the model.
It doesn't show any errors, something doesn't work when saving.
How, through the console or somehow, to find out the errors that are obtained there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kane, 2014-10-19
@IvanN777

The model you are saving has an errors method which contains an array of validation errors

class Person < ActiveRecord::Base
  validates :name, presence: true, length: { minimum: 3 }
end
 
person = Person.new
person.valid? # => false
person.errors.messages
 # => {:name=>["can't be blank", "is too short (minimum is 3 characters)"]}
 
person = Person.new(name: "John Doe")
person.valid? # => true
person.errors.messages # => {}

The documentation describes this in detail: Active Record Validations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question