0

I have the following route:

resources :foods do
collection do
   post :f01a
 end
end 

and controller:

def f01a
@user = User.find(params[:id])
@eat = Eat.find_by_user_id(@user)
@eat.toggle!(:f01)
redirect_to @user
end

and view:

<%= button_to "add f01a", send(:"f01a_foods_path", id: @user.id),  class: "btn btn-large btn-primary" %>

My goal is to call this controller action from a JavaScript onclick instead of using an HTML button.

The problem is that, currently, I can double click on the button and execute the code twice while the page is rendering. It's my understanding that JavaScript or AJAX fixes this.

2
  • Convert the button to a language? Can you please be more specific in what you require? Commented Sep 20, 2012 at 1:54
  • Neither Javascript nor AJAX will just "fix" that problem. You can have some JS to disable the button after it's been clicked so you can't click it again. Changing it from a button to a link or something won't make a difference. Commented Sep 20, 2012 at 2:46

2 Answers 2

1

First decorate the button with a class name specific to it:

<%= button_to "add f01a", send(:"f01a_foods_path", id: @user.id), class: "f01a btn btn-large btn-primary" %>

Then, put the following javascript in your application.js file and call it once the DOM is ready:

$('form .f01a').submit(function() {  
    $(":submit", this).attr("disabled", "disabled");   
    $(":submit", this).addClass("disabled");
});
Sign up to request clarification or add additional context in comments.

Comments

0

Angelo Chrysoulakis suggested good way to resolve your problem.

Also, if you want, then you can use masking on whole page which show some processing or waiting message to your user so that your user doesn't do any changes or click on other part of page till your process complete.

Here is good article or lib to achive this.

http://code.google.com/p/jquery-loadmask/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.