ruby-on-rails - 简单形式重复输入

标签 ruby-on-rails ruby simple-form

我正在使用 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:补丁

authenticity_token:m2rM8t/lZ1g3UfYER4xFiNkHKpDX5GrpjdapQCLPKHQ=

用户[receive_notifications]:0

用户[receive_notifications]:1

最佳答案

你不需要也不应该。这是一个隐藏值,用于解决浏览器执行未填写复选框的方式的问题。

问题是这样的:如果您只有一个(可见的)复选框,然后您不单击它...html 根本不会为该复选框发送值。

这不是您想要的...您想要的是如果用户没有单击复选框,您将收到值::user_receive_notifications => false

这就是 Rails 破解 html 的方式,如果复选框出现在页面上但未选中,则始终发送该值。

关于ruby-on-rails - 简单形式重复输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35425499/

相关文章:

ruby-on-rails - 如何断开与 Rails 中的特定数据库的连接?

ruby-on-rails - 继承的 Active Record 类的 link_to 问题

ruby-on-rails - 重新归档 simple_form 未定义方法附件字段

ruby-on-rails - 不可见的 CSS 注释

mysql - 使用空值作为 '' 还是 NULL 更好?

jquery - Sprockets::FileNotFound:找不到文件 'jquery-ui'?

ruby-on-rails - Rails 3.2 从不适用的复选框更新 bool 值

ruby - 如何播放 mp3 文件?

ruby-on-rails - 带有 acts-as-taggable-on 的简单表单自定义输入

ruby-on-rails - 使所有输入字段位于同一行