Rails 1 与 Rails 2 中的复选框
我正在将旧的 Rails 1 应用程序升级到 Rails 2,并且在处理复选框时遇到了问题。旧代码对每个复选框都有一个 check_box_tag
调用和相应的 hidden_field_tag
调用。虽然这之前有效,但升级后,params 哈希中的所有复选框都会显示为其默认隐藏值,无论是否选中它们。我一直在阅读有关 check_box
方法的内容,以及它在处理相应隐藏字段方面如何从 Rails 1 慢慢变为 2 到 3。
是否有任何关于 Rails 2 中复选框处理方式如何变化的文档,或者有人可以解释为什么 Rails 1 方法不再有效?
编辑:经过更多研究,我发现这个。看起来在 Rails 2.2 中参数提取总是获取给定键的第一次出现(因此隐藏字段应该放在最后),但在 Rails 2.3 参数提取中总是获取给定键的最后一次出现(因此隐藏字段应该放在最前面) )。发行说明和文档中显然对此没有详细记录。
I am upgrading an old Rails 1 application to Rails 2, and I'm running into a problem with the handling of check boxes. The old code has a check_box_tag
call and corresponding hidden_field_tag
call for each check box. While this worked before, after the upgrade all the checkboxes in the params hash come across as their default hidden value, regardless of whether they were checked. I've been reading about the check_box
method, and how it has slowly changed from Rails 1 to 2 to 3 in terms of working with the corresponding hidden field.
Is there any documentation out there on how check box handling changed in Rails 2, or can someone explain why the Rails 1 approach no longer works?
Edit: After more research, I found this. It looks like in Rails 2.2 parameter extraction always gets the first occurrence of a given key (so the hidden field should go last), but in Rails 2.3 parameter extraction always get the last occurrence of a given key (so the hidden field should go first). And this apparently is poorly documented in the release notes and docs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话,rails 2 不需要隐藏字段,您只需使用 check_box_tag 即可,它会为您完成所有操作。
If I remember right, you don't need to have an hidden field with rails 2, you just use check_box_tag and it does everything for you.