我正在使用ruby simple_form生成一个带有复选框的表单。该表单通过以下方式呈现:
<%= simple_form_for(@user) do |f| %>
<%= f.input :receive_notifications, as: :boolean, label: 'Receive Notifications' %>
<% end %>这将输出html:
<form accept-charset="UTF-8" action="/admin/users/9" class="simple_form edit_user" id="edit_user_9" method="post" novalidate="novalidate">
<div style="display:none">
<input name="utf8" type="hidden" value="✓">
<input name="_method" type="hidden" value="patch">
<input name="authenticity_token" type="hidden" value="m2rM8t/lZ1g3UfYER4xFiNkHKpDX5GrpjdapQCLPKHQ=">
</div>
<div class="form-group boolean optional user_receive_notifications">
<input name="user[receive_notifications]" type="hidden" value="0">
<label class="boolean optional control-label checkbox" for="receive_notifications">
<input checked="checked" class="boolean optional" id="user_receive_notifications" name="user[receive_notifications]" type="checkbox" value="1">Receive Notifications
</label>
</div>
</form>问题是,如何消除表单组中的重复输入?
编辑:来自浏览器检查器的表单数据:
utf8:✓
_method:patch
authenticity_token:m2rM8t/lZ1g3UfYER4xFiNkHKpDX5GrpjdapQCLPKHQ=
userreceive_notifications:0
userreceive_notifications:1
发布于 2016-02-16 14:45:16
这是一个隐藏的值,用于修复浏览器处理未填充复选框的方式的问题。
问题是:如果你只有一个(可见的)复选框,然后你没有点击它...html根本不会发送该复选框的值。
这不是你想要的..。您希望的是,如果用户没有单击复选框,您会收到值::user_receive_notifications => false
因此,这就是Rails破解html的方式,如果复选框出现在页面上,但没有选中,则总是发送该值。
https://stackoverflow.com/questions/35425499
复制相似问题