I was having the exact same problem (needed to disable and enable some fields according to user interaction). Solved it by using branch 5-1-stable
. (it was 5.1.2
before)
gem 'rails', git: 'https://github.com/rails/rails.git', branch: '5-1-stable'
and update your Rails gem
bundle update rails
This pull request solved the issue.
Note: The application I'm working on is not yet on production, so I don't have any problem now in using the stable
branch. If you need a more conservative approach maybe try using 5.1.4
instead, and check if the commit from the link above is included in the repo.
If you can't change your rails gem version, another solution could be to remove or to empty the name
attribute of the inputs that you don't
want to be submitted. If you check the change in the pull request you can see that currently rails-ujs
is removing from the params
those fields that have no name
attribute. So, removing/setting this attribute in the fields as you need them should work (I didn't try this though). Maybe something like this:
// 'disable' the fields
$('your-inputs-selector[disabled="disabled"]').each(function(idx, elem) {
var $elem = $(elem);
$elem.data('input-name', $elem.attr('name'));
$(elem).attr('name', '');
});
// 're-enable' the fields
$('your-inputs-selector').each(function(idx, elem) {
var $elem = $(elem)
$elem.attr('name', $elem.data('input-name'));
});
Hope this help.