为什么我的 Rails FormHelper (Formtastic) 无法正确显示布尔值?

发布于 2024-09-26 07:19:03 字数 591 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-10-03 07:19:03

如果删除 :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 be nil and not false?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文