San Backups
San Backups

Reputation: 503

Convert button to Javascript in Ruby on Rails

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.

Upvotes: 0

Views: 549

Answers (2)

GBD
GBD

Reputation: 15981

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/

Upvotes: 0

Angelo Chrysoulakis
Angelo Chrysoulakis

Reputation: 946

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");
});

Upvotes: 1

Related Questions