0

我想使用 Ajax 进行简单的删除,但这不起作用,我想要让 JQuery 淡出,但这不起作用,我删除并且 tr 不会消失。

我的 application.js 是

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

我的人员控制器是

  def destroy
    @person = Person.find(params[:id])
    @person.destroy

    respond_to do |format|
      format.html { redirect_to people_url }
      format.js   { render :nothing => true }  
    end
  end

我的观点 index.html.erb

  <tr>
    <td><%= person.name %></td>
    <td><%= person.birth %></td>
    <td><%= person.email %></td>
    <td><%= link_to 'Show', person %></td>
    <td><%= link_to 'Edit', edit_person_path(person) %></td>
    <td><%= link_to 'Destroy', person ,method: :delete, data: { confirm: 'Are you sure?' },:remote=> true, :class =>'delete_post'  %></td>
  </tr>
我正在学习 Ajax,但这不起作用,删除发生但 ajax 不起作用我在视图中包含此标签
<%= javascript_include_tag :defaults %>

但什么都不改变

日志是:

Started DELETE "/people/20" for 127.0.0.1 at 2012-09-03 15:23:45 -0300
Processing by PeopleController#destroy as JS
  Parameters: {"id"=>"20"}
  [1m[35mPerson Load (0.0ms)[0m  SELECT `people`.* FROM `people` WHERE `people`.`id` = ? LIMIT 1  [["id", "20"]]
  [1m[36mSQL (0.0ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (0.0ms)[0m  DELETE FROM `people` WHERE `people`.`id` = ?  [["id", 20]]
  [1m[36m (46.9ms)[0m  [1mCOMMIT[0m
  Rendered text template (0.0ms)
Completed 200 OK in 94ms (Views: 46.9ms | ActiveRecord: 46.9ms)
4

1 回答 1

1

问题是您的事件处理程序未附加到document处理ajax:success回调的对象。根据您的日志跟踪,您将获得 200 响应代码,并且该记录正在被删除。

在您的应用程序中尝试以下 JavaScript 代码:

$(document).on('ajax:success', '.delete_post', function(e) {
    $(e.currentTarget).closest('tr').fadeOut();
});

(参考:https ://stackoverflow.com/a/12221358/441611 )

于 2012-09-03T19:05:50.140 回答