gpt4 book ai didi

ruby-on-rails - Ruby on Rails 复选框不保存数据

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:01 25 4
gpt4 key购买 nike

我有一个复选框

<%= f.check_box :anonymous %>

我的表中有一列 anonymous 是真还是假。

在 html 中生成的代码:

 <input name="comment[anonymous]" type="hidden" value="0" />
<input id="comment_anonymous" name="comment[anonymous]" type="checkbox" value="1" />

现在,出于某种原因,当我添加数据时,无论我的匿名复选框是否被选中,它都不会保存。它不会更改数据库中的数据。除了匿名之外,所有其他字段都会被保存。

可能是什么问题?

最佳答案

改用#check_box_tag:

<%= check_box_tag(:anonymous) %>

来自官方指南:

Array parameters do not play well with the check_box helper. According to the HTML specification unchecked checkboxes submit no value. However it is often convenient for a checkbox to always submit a value. The check_box helper fakes this by creating an auxiliary hidden input with the same name. If the checkbox is unchecked only the hidden input is submitted and if it is checked then both are submitted but the value submitted by the checkbox takes precedence. When working with array parameters this duplicate submission will confuse Rails since duplicate input names are how it decides when to start a new array element. It is preferable to either use check_box_tag or to use hashes instead of arrays.

关于ruby-on-rails - Ruby on Rails 复选框不保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com