Rails 4: Disable submit button after click
Asked Answered
N

5

16

I have a form_tag(foo_path(@foo), remote: true, id: 'foo-form' form and a submit button submit_tag ("Submit", :id => "foo-submit")

I'd like to disable the submit button after it has been clicked. Obviously, I cannot use something like onlick="jQuery(this).prop('disabled', true);" because it would break the remote functionality. I'm aware of the :disable_with data attribute for the submit_tag but it doesn't seem to work for me. It does generate the right form code but it has no effect. I'm not sure why it doesn't work but it might be due to the fact that I use prototype (legacy reasons) and jquery at the same time. Though, I only load ujs for jquery and not for prototype. However, all other query_ujs features work very well.

Is there another way to disable the submit button?

Needlecraft answered 16/2, 2014 at 14:6 Comment(4)
Do you run jQuery.noConflict(); before all your jQuery commands? This may help. Can you confirm that all the ids are correct when you inspect the DOM and there are no duplicates?Gillispie
Yes to both. However, I managed to get `disable_with' to work by modifying my code. It might be due to a bug in the form helper, but I'm not sure yet.Needlecraft
Make sure you have all the needed libraries loaded, like jquery-rails etc. The data attribute "disable-with" should work in all cases. Or is there anything specific YOU do that it doesn't work?Allochthonous
@TanelSuurhans please see my answer belowNeedlecraft
N
-2

The problem was caused by invalid HTML code. See <div> breaks <form> disable_with works now

Needlecraft answered 16/2, 2014 at 16:56 Comment(1)
So essentially you issue was completely somewhere else and you did not provide all the code needed to debug this. Nor did you actually report the correct symptoms :) I hope this is a good lesson for you on debugging - always pinpoint your problem first and make sure the issue IS where you THINK it is.Allochthonous
M
61

use

submit_tag "Submit", id: "foo-submit", data: { disable_with: "Please wait..." }
Mclemore answered 16/2, 2014 at 15:10 Comment(1)
As I wrote in my post, the :disable_with data attribute doesn't work for meNeedlecraft
T
18

An alternative to the submit_tag is the button_tag. The button_tag allows you to add a font awesome spinner.

<%= button_tag 'Submit', class: 'btn btn-primary', 
      data: { disable_with: "<i class='fa fa-refresh fa-spin'>
      </i> Submitting Changes..."} %>
Tanked answered 24/5, 2015 at 1:23 Comment(0)
L
2

You use CoffeeScript to do something like these lines:

jQuery ->
  $('.theform').submit ->
    $('input:submit').attr("disabled", true)

This disables the form submit button when the form with class="theform" is submitted. Depending on your need you adjust this to fit with your class/id for the form.

Lashaun answered 16/2, 2014 at 14:13 Comment(2)
Doesn't seem to work. Just to be sure that my code is correct: jQuery('#foo-form').submit(function () { jQuery('#foo-submit').attr("disabled", true); }); Is that what you meant?Needlecraft
When the submit button is disabled the form won't submit under at least some browsers, and that's what the OP states as well.Boccherini
D
0

Since it is an ajax call, the resultant js.erb file from the corresponding action will be triggered.

The disable-on-click algorithm can be placed inside this js.erb file.

# in the controller
def foo_method
...
# will render js.erb for ajax call 
# assuming there is no .html view file in the directory
end

# in foo_method.js.erb
$('input:submit').attr("disabled", true);

It should bring you abck to the same page with the button noew disabled

Danika answered 15/8, 2014 at 2:39 Comment(0)
N
-2

The problem was caused by invalid HTML code. See <div> breaks <form> disable_with works now

Needlecraft answered 16/2, 2014 at 16:56 Comment(1)
So essentially you issue was completely somewhere else and you did not provide all the code needed to debug this. Nor did you actually report the correct symptoms :) I hope this is a good lesson for you on debugging - always pinpoint your problem first and make sure the issue IS where you THINK it is.Allochthonous

© 2022 - 2024 — McMap. All rights reserved.