Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnsome/768647 to your computer and use it in GitHub Desktop.
Save johnsome/768647 to your computer and use it in GitHub Desktop.
use jquery to insert hidden checkbox for unchecked check boxes
The rails check_box form helper inserts a hidden checkbox to handle unchecked check boxes. This doesn't work when the check box is within an array-like parameter - i.e. project[invoice_attributes][blah].
So - below will append a hidden form field with a value - so it can be handled in your controller or model etc.
for:
<%= check_box_tag("instance[data][blah]") %>
jquery:
$("#instance_form").submit(function() {
// insert hidden checkbox with value 0 - so unchecked checkboxes submit a value
$('input:checkbox').not(':checked').each(function() {
$("#instance_form").append("<input type='hidden' name='" + this.name + "' value='0' />");
});
return true;
});
@simanchen
Copy link

Thanks a lot

@lochhh
Copy link

lochhh commented Jul 19, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment