views:

177

answers:

2

I have a link_to_remote and I want to make sure people can only click it once while waiting for it to return.

Is there a good way to disable it after someone clicks it? (Changing the text of the link is nice too, but I want to disable it also to be sure).

This is Ruby on Rails btw.

+1  A: 

You can't disable a link, but you can change the href for example.

So you can use :before or :loading hooks to "disable" the link using javascript .

Edgard Arakaki
yep, but then it has to change it back after completing right? (unless the link itself gets replaced as part of the remote call). Anyone have a good example of this code?
Brian Armstrong
A: 

I ended up replacing the link in the :before block like Edgard suggested:

<div id="parent">
  <%= link_to_remote "Click Here",
    {:url => "/some_long_url",
    :method => :post,
    :before => "$('#parent').html('#{escape_javascript(link_to("Click Here"))}');"} %>
</div>

Note this uses JQuery. If you're using prototype you might need to change the '.html' method to the prototype equivalent ('.update' I believe).

Then after the AJAX call is made it redraws the link_to_remote with something like...

render :update do |page|
  page.replace_html  'parent', :partial => 'partial_containing_your_link_to_remote', :locals => {}
end

The link_to_remote in the first part should really be in that same partial to keep it DRY

Brian Armstrong