0

我有一个 rails 3.1 应用程序,我似乎无法通过 ajax 删除我的图像。我的代码示例是

def destroy
  @photo = Photo.find(params[:id])
  @photo.destroy
  respond_to do |format|
    format.html { redirect_to(user_photos_path(current_user)) }
    format.js   { render :nothing => true }
  end
end

那么在我看来我有

<%= link_to '(delete)', profile_photo_path(photo.profile, photo), :method => :delete if me %>

在我的 application.js 我有

$('.delete_post').bind('ajax:success', function() {
  $(this).closest('tr').fadeOut();
});

它对我不起作用。它执行动作但不会淡出

4

2 回答 2

0

在您的 ajax:success 回调this中,没有像您期望的那样绑定到 .delete_post 链接。也许您可以将 ID 添加到您的链接中,然后在您的控制器或模板中生成一些像$("#photo_17_delete").closest('tr').fadeOut();. 这只是处理它的一种方法的一个例子。

于 2011-06-14T01:49:39.603 回答
0

这是一个老问题,但我只是想指出,使用链接来修改或破坏您的数据的操作似乎是一种不好的做法。

您应该改用 to_button

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

于 2011-11-17T05:51:31.153 回答