我一直在寻找解决方案,但一直无法找出我的代码有什么问题。
我正在尝试在事件页面上使用 ajax 调用删除项目,但它似乎不起作用。
<%= link_to 'X', { :controller => 'items', :action => 'destroy', :id => item.id }, :method => :delete, :confirm => 'Are you sure you want to delete this item?', :remote => true %>
<a rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this item?" href="/items/83">X</a>
我相信一切看起来都正确,但是,数据属性似乎不起作用。当我单击它时没有确认发生,而是它调用控制器的“显示”操作,因此数据方法也不起作用。
最奇怪的部分是我在页面上生成了确认消息的其他内容。
<%= link_to image_tag("trashcan.png"), @event, :confirm => 'Are you sure you want to delete this event?', :method => :delete %>
<a rel="nofollow" data-method="delete" data-confirm="Are you sure you want to delete this event?" href="/events/3">
我尝试将第一个中的“X”更改为图像(以防 image_tag 成功) - 不走运。但是,当我将 link_to 更改为 button_to 时,它确实删除了,但没有确认我认为 button 不支持。所以我不确定我是否可能在其他地方有一些 javascript 错误,我认为不是这种情况,因为它适用于另一种情况,或者我可能在某处遗漏了一些东西。
编辑:我忘了提到我对每个项目也有评论,而且效果也很好......
<%= link_to 'X', [comment.item, comment], :confirm => 'Are you sure?', :method => :delete, :remote => true %>
<a rel="nofollow" data-remote="true" data-method="delete" data-confirm="Are you sure?" href="/items/83/comments/32">X</a>