为什么我的内容出现在使用JQuery Ajax刷新时不应该出现的地方?

时间:2013-01-09 16:18:48

标签: ruby-on-rails ruby-on-rails-3 jquery

在我的应用中,如果用户删除了评论,则会调用ajax并刷新页面而不加载。
但它正在重新加载整个_comment.html.erb部分,当它令人耳目一新时它看起来很奇怪。

例如,点击“删除确定”后,评论会弹出不应该出现的位置,并且会回到应有的位置。

删除工作正常。它的外观看起来并不好。

我希望有人了解我的应用会发生什么 表格内容之外应该是部分内容?
我该如何解决这个问题?

用户/ show.html.erb

...
<span id="comment">
    <%= render 'users/comment' %>
</span>
...

用户/ _comment.html.erb

<table>

  <tr>
    <th>ID</th>
    <th>Body</th>
    <th>Subject</th>
    <th>Posted by</th>
    <th>Delete</th>
  </tr>

 <% @comments.each do |comment| %>
  <tr id="<%= comment_dom_id(comment) %>">
    <td><%= comment.id %></td>
    <td><%= comment.body %></td>
    <td><%= comment.subject %></td>
    <td><%= comment.user.name if comment.user %></td>
    <td>
    <%= button_to 'destroy', polymorphic_path([@user, comment]), :data => {:confirm => 'confirm'}, :method => :delete, :disable_with => 'deleting...', :remote => true, :class => 'btn btn-danger' if current_user && current_user.id == comment.user_id %>
    </td>
  </tr>
 <% end %>

</table>

更新:

用户/ refresh.js.erb

$('#comment').html("<%= j(render(:partial => 'users/comment')) %>");

0 个答案:

没有答案