Is there a :disable_with equivalent for link_to_re

2019-04-11 22:02发布

问题:

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:

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 .



回答2:

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