From aaf5e9e324b4134516a644afacc90f0aa2f5e996 Mon Sep 17 00:00:00 2001 From: Youri van der Lans Date: Wed, 24 Oct 2018 11:06:52 +0200 Subject: [PATCH] Submit empty value for unchecked radio/checkbox input. --- src/rails.js | 3 +++ test/public/test/data-remote.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/rails.js b/src/rails.js index 93620134..ba5fc933 100644 --- a/src/rails.js +++ b/src/rails.js @@ -131,6 +131,9 @@ method = element.data('method'); url = element.data('url'); data = element.serialize(); + if (element.is('input[type=checkbox],input[type=radio]')) { + data = element.attr('name') + '=0&' + data; + } if (element.data('params')) data = data + '&' + element.data('params'); } else if (element.is(rails.buttonClickSelector)) { method = element.data('method') || 'get'; diff --git a/test/public/test/data-remote.js b/test/public/test/data-remote.js index 215d8217..24501120 100644 --- a/test/public/test/data-remote.js +++ b/test/public/test/data-remote.js @@ -143,6 +143,34 @@ asyncTest('changing a select option with data-remote attribute', 5, function() { .trigger('change'); }); +asyncTest('submitting an unchecked checkbox with data-remote attribute submits input with value 0', 3, function () { + $('#qunit-fixture') + .append($('')); + + $('input[type="checkbox"][data-remote]') + .on('ajax:success', function (e, data, status, xhr) { + App.assertCallbackInvoked('ajax:success'); + App.assertRequestPath(data, '/echo'); + equal(data.params.user.active, '0', 'ajax arguments should have key active with value 0'); + }) + .on('ajax:complete', function () { start() }) + .trigger('change'); +}) + +asyncTest('submitting an unchecked radio button with data-remote attribute submits input with value 0', 3, function () { + $('#qunit-fixture') + .append($('')); + + $('input[type="radio"][data-remote]') + .on('ajax:success', function (e, data, status, xhr) { + App.assertCallbackInvoked('ajax:success'); + App.assertRequestPath(data, '/echo'); + equal(data.params.user.active, '0', 'ajax arguments should have key active with value 0'); + }) + .on('ajax:complete', function () { start() }) + .trigger('change'); +}) + asyncTest('submitting form with data-remote attribute', 4, function() { $('form[data-remote]') .on('ajax:success', function(e, data, status, xhr) {