2

我有以下代码:

<% form_for(@libation) do |f| %>
  <%= f.radio_button :carbonated, true  %> <%= f.label :carbonated, "Yes" %>
  <%= f.radio_button :carbonated, false %> <%= f.label :carbonated,  "No" %>
<% end %>

数据库中的carbonated列是类型tinyint
当上面运行并单击时,No我会看到一个大红框告诉我:

1 error prohibited this libation from being saved
  There were problems with the following fields:
  * Carbonated can't be blank

有任何想法吗?
-谢谢

4

2 回答 2

16

我在验证布尔值时遇到了困难,所以我学会了验证它们的存在,你需要做:

validates_inclusion_of :carbonated, :in => [true, false]
于 2009-12-03T11:09:30.110 回答
-5

哈,回答我自己的问题。似乎 HTML 规范没有正确发送“假”存在问题,因此需要一些解决方法。对我来说,在模型文件中我有:

validates_presence_of :carbonated

只需将其删除即可修复此错误消息。我默认为false,可以更改为true。错误信息消失了。

再见。

于 2009-07-24T18:10:57.613 回答