我目前正在尝试在 Rails 3 中插入一些简单的真/假单选按钮,但我找不到让单选按钮插入“假”的方法。
我的代码如下:
<%= f.radio_button :accident_free, true %><label for="auction_accident_free_true">ja</label>
<%= f.radio_button :accident_free, false %><label for="auction_accident_free_false">nein</label>
我已经尝试过:
但对于 false 值似乎没有任何效果。我的字段设置为
validates_presence_of :accident_free
当单击 false 按钮时,我总是收到必须填写才能继续的消息。单击 true 按钮时,它工作正常,但 false 不会被识别。
有谁知道如何正确地做到这一点?
提前致谢
阿恩
请您参考如下方法:
就是这个:
http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_presence_of
validates_presence_of()
validates that the specified attributes are not blank (as defined byObject#blank?
)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]
This is due to the way
Object#blank?
handles boolean values:false.blank? # => true
我使用脚手架和
"1"
尝试了您的示例和
"0"
如
<%= f.radio_button :foo, "0" %>
<%= f.radio_button :foo, "1" %>
他们工作了。