【问题标题】:By default simple_form - collection_check_boxes displays hidden_field at the end默认情况下 simple_form - collection_check_boxes 在末尾显示 hidden_​​field
【发布时间】:2013-12-23 13:20:14
【问题描述】:

我在 new.html.erb 中使用 simple_form 的 collection_check_boxes。默认情况下,当呈现 html 时,它会在末尾添加隐藏字段,因为在控制器端我得到了最后一个元素为“”的值数组。谁能帮助我如何防止隐藏字段在视图上呈现?

new.html.erb:

<%= f.collection_check_boxes :topic_id, Topic.all, :id, :name %>
<%= f.button :submit %>

我使用 Firebug 检查了我的复选框元素,结果如下:

    <span>
    <span>
    <input type="hidden" value="" name="revision[topic_id][]">
    <input class="btn" type="submit" value="Create Revision" name="commit">

我想删除上面的隐藏字段。请帮忙。

【问题讨论】:

  • 为什么要删除它?据我了解,这是为了涵盖您根本没有选择任何复选框的情况......
  • 我基本上需要使用那些topic_ids并对其执行计数和类似功能。如果我有一个空字符串,那么我必须在使用它之前设置一个条件或将其弹出。但基本上我想知道目的。谢谢你:)

标签: ruby-on-rails checkbox ruby-on-rails-4 simple-form


【解决方案1】:

对于那些确实想要删除空值的人,请使用include_hidden: false param:

<%= f.collection_check_boxes :topic_id, Topic.all, :id, :name, include_hidden: false %>

它目前没有记录在 API 中,但它在那里:https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/tags/collection_check_boxes.rb

      # Append a hidden field to make sure something will be sent back to the
      # server if all check boxes are unchecked.
      if @options.fetch(:include_hidden, true)
        rendered_collection + hidden_field
      else
        rendered_collection
      end

【讨论】:

    【解决方案2】:

    隐藏字段是故意存在的,您可能不想删除它。用于处理没有选中复选框的情况。

    我认为 simple_form 最终会退回到标准 FormBuilder 中相同的基本 check_box 逻辑。以下是有关复选框为何具有该隐藏字段的一些文档。来自check_box documentation

    # The HTML specification says unchecked check boxes are not successful, and
    # thus web browsers do not send them. Unfortunately this introduces a gotcha:
    # if an +Invoice+ model has a +paid+ flag, and in the form that edits a paid
    # invoice the user unchecks its check box, no +paid+ parameter is sent. So,
    # any mass-assignment idiom like
    #
    #   @invoice.update(params[:invoice])
    #
    # wouldn't update the flag.
    #     
    # To prevent this the helper generates an auxiliary hidden field before
    # the very check box. The hidden field has the same name and its
    # attributes mimic an unchecked check box.
    #
    # This way, the client either sends only the hidden field (representing
    # the check box is unchecked), or both fields. Since the HTML specification
    # says key/value pairs have to be sent in the same order they appear in the
    # form, and parameters extraction gets the last occurrence of any repeated
    # key in the query string, that works for ordinary forms.
    

    标准collection_check_boxesexample显示隐藏字段:

    # Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):
    #   collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
    #
    # If <tt>@post.author_ids</tt> is already <tt>[1]</tt>, this would return:
    #   <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
    #   <label for="post_author_ids_1">D. Heinemeier Hansson</label>
    #   <input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
    #   <label for="post_author_ids_2">D. Thomas</label>
    #   <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
    #   <label for="post_author_ids_3">M. Clark</label>
    #   <input name="post[author_ids][]" type="hidden" value="" />
    

    【讨论】:

    • 嘿,谢谢 :) 我基本上想知道文档的原因,而这正是你所做的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 2018-06-17
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多