为什么我的 Rails FormHelper (Formtastic) 无法正确显示布尔值?
我正在使用 Formtastic 并且有一个简单的布尔字段。在我看来,我把
<%= f.input :active, :label => "Enabled?", :as => :select, :include_blank => false, :collection => [ ["Yes", true], ["No", false] ] %><br />
它保存到数据库就好了。但是当它加载时,它总是显示第一个值(“是”)。
我缺少什么?当该字段为 false 时,它应默认为“否”。
感谢您的任何提示。
编辑
当我把[“否”,假]放在第一位时,它起作用了!
<%= f.input :active, :label => "Enabled?", :as => :select, :include_blank => false, :collection => [ ["No", false], ["Yes", true] ] %>
为什么这很重要?
I am using Formtastic and I have a simple boolean field. In my view, I have
<%= f.input :active, :label => "Enabled?", :as => :select, :include_blank => false, :collection => [ ["Yes", true], ["No", false] ] %><br />
It saves to the database just fine. But when it loads, it always shows the first value ("Yes").
What am I missing? It should default to "No" when the field is false.
Thanks for any tips.
EDIT
When I put the ["No", false] first, it works!
<%= f.input :active, :label => "Enabled?", :as => :select, :include_blank => false, :collection => [ ["No", false], ["Yes", true] ] %>
Why would that matter?????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是关于主题
https://rails.lighthouseapp.com/projects/8994/tickets/5702-options_for_select-do-not-select-boolean-values- Correctly
并在rails >= v3.0.3 中修复
Here is a submitted bug/patch on the topic
https://rails.lighthouseapp.com/projects/8994/tickets/5702-options_for_select-do-not-select-boolean-values-correctly
and it is fixed in rails >= v3.0.3
如果删除
:include_blank => 会发生什么?假的?是否选择了第一个选项(空白)?如果是,该属性的值是否可以为
nil
而不是false
?What happens if you remove the
:include_blank => false
? Is the first option (blank) selected? If so, could the value for the attribute benil
and notfalse
?