Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

By default simple_form - collection_check_boxes displays hidden_field at the end

I am using simple_form's collection_check_boxes in my new.html.erb. By default, when the html is rendered, it adds hidden field at the end due to which at controller side I get array of values with last element as "". Can anyone please help me how can I prevent the hidden field to be rendered on View?

new.html.erb :

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

I used Firebug to inspect my checkboxes element and following was the outcome :

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

I want to remove the above hidden field. Please help.

like image 623
Disha Avatar asked Dec 23 '13 13:12

Disha


2 Answers

For those who do want to remove the empty value, use include_hidden: false param:

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

It's not currently documented in the API, but it's there: 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
like image 199
Hnatt Avatar answered Oct 15 '22 21:10

Hnatt


The hidden field is there intentionally and you probably don't want to remove it. It is to handle the case when no check boxes have been selected.

I think that simple_form ends up falling back on the same basic check_box logic that is in the standard FormBuilder. Here is some documentation for why check boxes have that hidden field. From the 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.

And the example by the standard collection_check_boxes shows the hidden field:

# 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="" />
like image 25
cschroed Avatar answered Oct 15 '22 20:10

cschroed