我有 ajax 评论提交和删除,提交完美但删除不起作用。
以下是我的代码:-
控制器
def destroy
@status_update = StatusUpdate.find(params[:id])
if @user == current_user
if @status_update.destroy
respond_with do |format|
format.html do
if request.xhr?
render :partial => "users/post_status_update", :locals => {:user => @user}, :layout => false, :status => :created
else
redirect_to @user
end
end
end
end
end
end
Javascript
$(document).ready(function(){
$('.delete-status-update')
.on("ajax:beforeSend", function(evt, xhr, settings){
})
.on("ajax:success", function(evt, data, status, xhr){
$('.status-partial').replaceWith(xhr.responseText);
})
});
看法
.status-partial
-@user.status_updates.latest.limit(5).each do |status_update|
.row-fluid.cycle-status-update{:class => cycle("dark", "light")}
.row-fluid
.span11
= status_update.status
.span1
.delete-status-update-link
%strong= link_to "X", [@user,status_update], :remote => true, :method => :delete, :class => "delete-status-update"
.row-fluid
.time-ago-in-words
="#{time_ago_in_words(status_update.created_at)} ago"
问题是,一旦我单击删除链接,状态部分没有被替换,在刷新页面 ajax 删除第一次工作后,它再次停止工作。