Home>
I want to solve

I'm making a web app in Rails, but I have multiple checkboxes and I want to hide them for a day if I check them.
I tried it myself using jquery cookie, but it doesn't work because it is hidden or not.

If i understand, I would like you to lend me your wisdom.
Thank you.

Corresponding source code

html file

<% = f.collection_check_boxes: habit_ids, @ user.habits ,: id ,: task, checked: false do | b |%>

<% = b.label do%>
<% = b.check_box%>
<% = b.text%>


jquery file

$(document) .on ('turbolinks: load', function () {
    if ($.cookie ('complete')) {
     var cookie = ($.cookie ('complete'))
     var habit_class = cookie.split (',');
    $(habit_class) .each (function (index, element) {
     $(element) .hide ();
    });
    } else {
       var clicks = [];
    }
    $('input [name = "user [habit_ids] []"]'). one ('click', function () {
    var p = $('<p>'). text ('achieved!')
    $(this) .before (p);
    $(this) .hide ();
   $('input [name = "user [habit_ids] []"]: checked'). each (function () {
     clicks.push ('.one-click' + this.value);
   });
   $.cookie ('complete', clicks, {expirations: 1, path:'/'});
});
});
error contents
Uncaught TypeError: Cannot read property'push' of undefined
    at HTMLInputElement.  (check.js: 18)
    at HTMLInputElement.fn (event.js: 84)
    at HTMLInputElement.dispatch (event.js: 328)
    at HTMLInputElement.elemData.handle


It seems that push is not working.

  • Answer # 1

    When taking out complete, it is divided by "," as a character string
    Isn't the array mama processed at the time of input?
    Whether there is a way to join or json

  • Answer # 2

    I saw the codeif ($.cookie ('complete')) {Only when is falseclicksVariable is not defined. First of all, take a look from that point.