Pyrchev
Pyrchev

Reputation: 21

Rails forms: after click - disable link

I have more forms in may rails apps with standart 'Save' and 'Update' buttons. How to make this button non-active (disabled) after first click to this link?

Upvotes: 2

Views: 5181

Answers (4)

User16119012
User16119012

Reputation: 957

This is soo late but some one my found it useful.

Some times :disable_withdoes not work in rails. For this u've to check if jquery-rails gem is added in your gemfile or not.

Then use 'data-disable-with' => "SUBMITTING" option.

Upvotes: 0

Ninjabiscuit
Ninjabiscuit

Reputation: 134

If you want to do it with javascript, then why not use event delegation?

$(document.body).delegate("form.active","click",function(){
    this.className = "clicked";
});

This delegates a click event to every form in the page with a class of active (equally you could limit this to every form in a container by using a different context ie $("#container").delegate(...) or omit the active class) and changes the class from being active to clicked

$(document.body).delegate("form.clicked","submit",function(e){
    e.preventDefault();
});

This delegates a submit event to every form on the page with a className of clicked and stops the form from being submitted.

Alternatively, you could use the first delegate function to bind a submit event directly to the form itself (so replace both of the above functions with the following).

$(document.body).delegate("form","click",function(){
    $(this).submit(function(e){
        e.preventDefault();
        e.stopPropagation();
    })
});

Upvotes: 0

felix
felix

Reputation: 11542

You can use the disable_with option with the form_tag and form_for helpers In the case of form_tag, you can use

submit_tag "Complete sale", :disable_with => "Please wait..."

In the case of form_for, you can use

   f.submit 'Create', :disable_with => "Creating..."

source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

Upvotes: 19

Amir Raminfar
Amir Raminfar

Reputation: 34150

With jquery you can make it really easy

$('form').submit(function(){
$('input[type=submit]', this).attr('disabled', 'disabled');
});

The above looks for ALL forms and makes the submit button disabled after clicking submit. This is the preferred method because the enter button would also triggers a submit.

Upvotes: 2

Related Questions