Answer the question
In order to leave comments, you need to log in
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
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 # => {}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question