问题描述
|
我有一个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();
});
它对我不起作用。它执行动作,但不会消失
解决方法
在您的ajax:success回调中,
this
未像您期望的那样绑定到.delete_post链接。也许您可以在链接中添加ID,然后在控制器或模板中生成一些JavaScript,例如$(\"#photo_17_delete\").closest(\'tr\').fadeOut();
。那只是处理它的一种方式的例子。
, 这是一个古老的问题,但我只想指出,使用链接到修改或破坏您的数据的操作似乎是一种不好的做法。
您应该改用to_button
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to