将选择形式的选项与布尔值匹配 [英] Match an option in select form with boolean values

查看:48
本文介绍了将选择形式的选项与布尔值匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

%br
= f.label :active, 'Status'
= f.select :active, ['Active','Inactive']

Symbol:active是一个布尔型var.我该如何匹配Active => 1/True和Inactive => 0/False来进行数据库添加.

Symbol :active is a boolean type var. How can i match Active => 1/True, and Inactive => 0/False , for the database adding.

对不起,对于新手问题,但我不知道.

Sorry for the newbie question, but i can't figure it out.

推荐答案

您可以为每个选项提供一对值:第一个将用作标签(<option>标签的内部文本),第二个将用作标签value属性:

You can provide a pair of values for each options: first will be used as label (inner text of <option> tag), second will be used as a value attribute:

= f.select :active, [['Active', true], ['Inactive', false]]

它将呈现如下内容:

<select name="model[active]">
  <option value="true">Active</option>
  <option value="false">Inactive</option>
</select>

查看 select 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆