How to get blank checkboxes to pass as false to params

后端 未结 5 2164
北海茫月
北海茫月 2021-02-01 01:35

I have a form that is an update user form where several of the elements are checkboxes. I want true to pass to the params if checked (this is working) and false to pass to the

相关标签:
5条回答
  • 2021-02-01 02:18

    If anyone have column type boolean and using check_box_tag then look at this. It worked for me. <%= hidden_field_tag :basketball, 'false' %> <%= check_box_tag :basketball, true, is_checked? %>

    0 讨论(0)
  • 2021-02-01 02:18

    Look at it

    The main idea to place hidden field before checkbox. When a form is submitted fields will be parsed: if checkbox is checked - its value will be passed, else the value of hidden field

    For example smth like this (NOT testes)

    <%= hidden_field_tag :basketball, false %>
    <%= check_box_tag :basketball, checked = true %> Basketball</br></br>
    
    0 讨论(0)
  • 2021-02-01 02:20

    The problem is if you use a hidden attribute before a checkbox and give it the same reference like this:

    <%= hidden_field_tag :basketball, false %>
    <%= check_box_tag :basketball, checked = true %> Basketball</br></br>
    

    Then if you want to do any type of dynamic action on an attribute, say like $("#basketball").removeAttr("checked") it will target the first instance of $("#basketball") which in this case is the hidden object.

    What I ended up doing in my implementation was to add a default value of false to my checkboxes since my default setting was to be unchecked. Then in my jquery I detected a change on the checkbox elements and grabbed the ID of that checkbox. When a change is detected I evaluated the checked property for true or false and set the value attribute accordingly.

    $(".checkbox").on('change', function(e) {
      var that;
      that = "#" + e.target.getAttribute("id");
      if ($(that).prop("checked") === true) {
        $(that).val("true");
      }
      if ($(that).prop("checked") === false) {
        return $(that).val("false");
      }
    });

    When I submit my PUT method form action it is updating the database record correctly with the boolean value even if it is blank.

    0 讨论(0)
  • 2021-02-01 02:27

    You need to place a hidden field tag before each checkbox with an empty value, for example:

    <%= hidden_field_tag :basketball, '' %>
    <%= check_box_tag :basketball, checked = true %> Basketball</br></br>
    

    Then the form is aware it needs to populate that field with an empty value if nothing is selected.

    0 讨论(0)
  • 2021-02-01 02:27

    It appears that you are able to pass the value. See the following code:

    check_box_tag 'accept'
    <input id="accept" name="accept" type="checkbox" value="1" />
    
    check_box_tag 'rock', 'rock music'
    <input id="rock" name="rock" type="checkbox" value="rock music" />
    
    check_box_tag 'receive_email', 'yes', true
    <input checked="checked" id="receive_email" name="receive_email" type="checkbox" value="yes" />
    
    check_box_tag 'tos', 'yes', false, :class => 'accept_tos'
    <input class="accept_tos" id="tos" name="tos" type="checkbox" value="yes" />
    
    check_box_tag 'eula', 'accepted', false, :disabled => true
    <input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />
    

    http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag

    0 讨论(0)
提交回复
热议问题