两个布尔字段。单一输入字段。红宝石 on Rails
我的模型中有两个布尔字段。存在一种依赖性,即一次只能有一个为真。在我的视图页面上,我想使用 formattastic 将它们显示为无线电输入。不是单独的,而是作为一个无线电组。我知道有一条轨道,但不知何故我找不到它。
请帮忙。提前致谢。
I have two boolean fields in my model. There is a dependency that only one can be true at a time. On my view page I want to display them as a radio inputs using formtastic. And not separately but as a single radio group. I know there is a rails way but somehow I am not able to find it.
Please help. Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个虚拟属性来获取和设置适当的值。例如,如果您有一个名为male 的布尔值和一个名为female 的布尔值,则可以使用性别属性来控制两者,如下所示:
然后您可以有一个用于选择性别的单选按钮组:
当然,您始终可以选择组合这些值转换为单个三态属性,例如性别,可以是“M”、“F”或 NULL。
You could create a virtual attribute to get and set the appropriate value. For example, if you had a boolean named male and a boolean named female, you could control both with the gender attribute like this:
You could then have a radio button group for selecting gender:
Of course you always have the option of combining the values into a single 3-state attribute, like gender which can be 'M', 'F' or NULL.