Well, I'm building a RESTfull API using rails 4.2.6 and ruby 2.3.0.
I've an :programs resources and whenever I try to make an POST request through Google Postman, I'm receiving a "can't be blank" error.
Wait, let me illustrate...
When I send this, through Postman:
"can't be blank"
So you have this in your model:
validates :expiration_points, presence: true
So what does
presence: true do? We could check the
validates documentation but that just says:
This method is a shortcut to all default validators and any custom validator classes ending in 'Validator'. Note that Rails default validators can be overridden inside specific classes by creating custom validator classes in their place such as PresenceValidator.
but that just gives us a bunch of examples and tells us what some of the options do. You could look for
PresenceValidator documentation but there's no such thing.
If you'd been using Rails for awhile, you'd know that
validate :f, :presence => true
is functionally equivalent to:
validates_presence_of documentation is still around:
Validates that the specified attributes are not blank (as defined by
If you want to validate the presence of a boolean field (where the real values are
false), you will want to use
validates_inclusion_of :field_name, in: [true, false].
This is due to the way
Object#blank?handles boolean values:
false.blank? # => true.
So you don't want
presence: true at all, you want to use
in: [ true, false ] instead:
validate :expiration_points, :in => [ true, false ]