Ruby on Rails 4 表单单选按钮的验证
Ruby on Rails 4 Validation of Form Radio button
我有一个带有单选按钮的表单,其中有四个不同的选项。我想确保用户在提交表单之前至少选择了一个单选按钮。现在,我正在手动执行此操作,因为我无法进行验证。我更愿意找出一种使用验证的方法,这样可以在提交表单之前在客户端完成。有没有人有验证单选按钮的经验?
查看:
<tr>
<td width="30%" align="center" style="color:white;"><%= f.radio_button :file_type, "1" %>Type 1</td>
<td width="25%" align="center" style="color:white;"><%= f.radio_button :file_type, "2" %>Type 2</td>
<td width="20%" align="center" style="color:white;"><%= f.radio_button :file_type, "3" %>Type 3</td>
<td width="25%" align="center" style="color:white;"><%= f.radio_button :file_type, "4" %>Type 4</td>
</tr>
在我的模型中,我尝试了以下代码,但这没有用:
validates :file_type, presence: true
感谢任何帮助!
使用 validates_inclusion_of 验证模型中的 file_type
:
validates_inclusion_of :file_type, :in => 1..4
我有一个带有单选按钮的表单,其中有四个不同的选项。我想确保用户在提交表单之前至少选择了一个单选按钮。现在,我正在手动执行此操作,因为我无法进行验证。我更愿意找出一种使用验证的方法,这样可以在提交表单之前在客户端完成。有没有人有验证单选按钮的经验?
查看:
<tr>
<td width="30%" align="center" style="color:white;"><%= f.radio_button :file_type, "1" %>Type 1</td>
<td width="25%" align="center" style="color:white;"><%= f.radio_button :file_type, "2" %>Type 2</td>
<td width="20%" align="center" style="color:white;"><%= f.radio_button :file_type, "3" %>Type 3</td>
<td width="25%" align="center" style="color:white;"><%= f.radio_button :file_type, "4" %>Type 4</td>
</tr>
在我的模型中,我尝试了以下代码,但这没有用:
validates :file_type, presence: true
感谢任何帮助!
使用 validates_inclusion_of 验证模型中的 file_type
:
validates_inclusion_of :file_type, :in => 1..4