14

How can I manually re-enable links (not form elements) that get disabled with Rails' disable_with feature?

bevanb
  • 8,201
  • 10
  • 53
  • 90

7 Answers7

14

The call to reenable links is slightly different than form elements. It actually binds a handler to the click event that stops anything else from happening. I was able to figure this out by investigating how the jquery-ujs library.

To reverse this effect, simply use the enableElement method on your jQuery object:

$.rails.enableElement($('a[data-disable-with]'));


With Turbolinks, it also helps to watch for the 'page:change' event instead of window.unload:

$(document).on('page:change', function() {
   $.rails.enableElement($('a[data-disable-with]'));
});
Matthew
  • 141
  • 1
  • 2
9

A solution I found here:

$(window).unload(function() {
  $.rails.enableFormElements($($.rails.formSubmitSelector));
});
DGM
  • 26,629
  • 7
  • 58
  • 79
  • 1
    Yup, this works in Rails 3.2. Essentially, just pass a jQuery object or objects to `$.rails.enableFormElements` and it will re-enable it. In my case I passed the form I was submitting: `$.rails.enableFormElements( $( 'div.modal form' ) )` – Joshua Pinter May 13 '18 at 01:43
5

Rails has updated their javascript to no longer use jQuery.

You can now re-enable elements with the following (assuming you are still using jQuery):

var selectors = [Rails.linkDisableSelector, Rails.formEnableSelector].join(', ');
$(selectors).each(function() {
  Rails.enableElement(this);
})
gabeodess
  • 2,006
  • 21
  • 13
2

Hey its quite simple you just need to find button and do

$button = $('#someId')
$.rails.enableElement($button)
$button.removeAttr('disabled')
waqas ali
  • 603
  • 6
  • 13
1

Based on @DGM solution I ended up with the following code:

$.rails.enableFormElements($disabled_button);

Where:

$disabled_button is the jQuery object for the button disabled by data-disable-with which could be selected like this:

$disabled_button = $('[data-disable-with]');
jmarceli
  • 19,102
  • 6
  • 69
  • 67
0

OK I found this interesting work around (apparently the problem is only in FF) set :autocomplete => 'off' and now it works. Or one of the other answer might work as well.

ref: https://github.com/rails/jquery-ujs/issues/357

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
-1

You can use jQuery to remove the data-disable-with attribute that Rails adds to the button: $('#disabledbutton').removeAttr('data-disable-with');