Rails link_to disable_with然后删除链接

时间:2015-03-19 03:25:42

标签: jquery ruby-on-rails ajax link-to

我正在使用Link_to使用ajax向我的页面添加一些内容,但我只希望用户这样做一次。我这样做是通过用js.erb文件中的新html元素替换链接。这一切都正常如下:

<%= link_to(expand_task_subset_user_path(user, li_id: li_id, span_id: span_id, user_tasks_to_expand_sym: user_tasks_to_expand_sym),
    {:method => :get, remote: true, 'data-mahi-link-type'=>'expand_task_subset'}) do %>
    <i class="glyphicon glyphicon-plus-sign" title="Expand this branch"></i>
<% end %>

js.erb文件片段替换上面创建的链接:

$("#<%=@span_id%> > a:has(i)").replaceWith("<i class=\"glyphicon glyphicon-minus-sign\" title=\"Collapse this branch\"></i>")

我今天注意到,在js.erb文件中的所有内容都已完成之前,可以单击链接两次,这意味着它会插入其他内容两次。我认为解决这个问题的显而易见的方法是添加一个像这样的disable_with:

<%= link_to(expand_task_subset_user_path(user, li_id: li_id, span_id: span_id, user_tasks_to_expand_sym: user_tasks_to_expand_sym),
    {:method => :get, remote: true, 'data-mahi-link-type'=>'expand_task_subset', data: { disable_with: 'loading...' }}) do %>
    <i class="glyphicon glyphicon-plus-sign" title="Expand this branch"></i>
<% end %>

问题(我认为)是disable_with暂时更改了html,jquery选择器$("#<%=@span_id%> > a:has(i)")找不到要替换的任何匹配元素。我已经尝试禁用链接作为js.erb文件中的第一个语句,但如果你很快,你仍然可以击败它并点击两次。有没有其他方法可以在点击后直接禁用链接?

更新:disable_with会替换&#39; a&#39;的内容。标记,所以我只是将我的jquery选择器更改为:

$("#<%=@span_id%> > a[data-mahi-link-type='expand_task_subset']")

而不是寻找孩子&#39; i&#39;标签,现在一切都很棒!

1 个答案:

答案 0 :(得分:0)

你可以做的是使用Jquery一旦点击就禁用链接,如下所示:

$('#Link').click(function(a) {
    a.preventDefault();
    //other goods
});