Is there a :disable_with equivalent for link_to_remote?

青春壹個敷衍的年華 提交于 2019-12-21 20:38:59

问题


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



来源:https://stackoverflow.com/questions/2358469/is-there-a-disable-with-equivalent-for-link-to-remote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!