所以,我有一个表单,使用 boolean 值来选择男性或女性。当我使用 validates :presense 作为 boolean 字段时,它会返回,性别不能为空!当我删除验证部分时,它让它以真或假的形式传递到数据库中。它不会让它为零。我假设这是一个问题,因为我使用 t/f 但我似乎不明白为什么。这是我的模型
class Visit < ActiveRecord::Base
validates :user_id, :first_name, :last_name, :birthdate, :gender,
presence: true
end
以及我对该领域的看法
<%= f.select :gender,
[['Male',false],['Female',true]], :value => false,
label: "Gender" %>
最佳答案
因为您无法对 boolean 字段使用存在验证。请改用包容性。请参阅此处的文档: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].)
关于ruby-on-rails - Rails validates_presense 未验证 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27002946/