So, I have a form, using a boolean to select male or female. When i use validates :presense for the boolean fields, it returns back, gender cannot be blank! Wehn I remove the validates portion, it lets it pass through as true or false into the DB. It doesn't leave it nil. I'm assuming this is an issue because im using t/f but I can't seem to figure out why. Here is my model
class Visit < ActiveRecord::Base
validates :user_id, :first_name, :last_name, :birthdate, :gender,
presence: true
end
And my view for the field
<%= f.select :gender,
[['Male',false],['Female',true]], :value => false,
label: "Gender" %>
Because you can't use the presence validation on boolean fields. Use inclusion instead. See the documentation here: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html
If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, in: [true, false].)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With