本文介绍了javascript(jquery-rails)在rails 3.1.rc5中无法完全正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次通过javascript删除对象时,我都使用remove方法不起作用.

every time i use the remove method when deleting object via javascripts it does not work.

我的destroy.js代码如下.

my destroy.js code is as follows.

$("#<%= dom_id(@comment) %>").remove();
    $('#comment-count-<%= @parent.id %>').html("<%= pluralize(@parent.comments.count,'comment')%>");

复数形式很好

我的_comment.html.erb如下

my _comment.html.erb is as follows

    <%= div_for comment do %>
<% if comment.user.profile.icon.exists? %>
     <%= image_tag comment.user.profile.icon.url(:small), :alt => comment.user.name, :class => "avatar tip", :title => comment.user.name %>
   <% else %>
   <%= image_tag comment.user.gender == 'Female' ? 'missing_woman.png' : 'missing_thumb.png', :alt => comment.user.name, :class => "avatar tip", :title => comment.user.name %>
   <% end %>
  <div class="comment-container">
     <span><%= link_to comment.user.name, comment.user.profile, :class => "profile-link-small" %></span> <%= comment.comment%>
   <div><span style="color:#888;"><%= formatted_time(comment.created_at) %></span> &ndash;&ndash; <span class="rank"><%= comment.user.level %></span>
    <% if can? :update, comment%> |
<span class="del"><%= link_to "Delete", comment, :confirm => "Are you sure you want to delete this comment?", :method => :delete, :remote => true %></span>
  <% else %>
        <span class="dialog_link">Delete</span>
  <% end %>
     </div>
  </div>
  <div class="clear"></div>
<% end %>

它曾经可以工作,但在我更新到rails rc5之后它就存储了

It use to work before but after i updated to rails rc5 it storped

推荐答案

您的jQuery这样说:

Your jQuery says this:

$("#<%= dom_id(@comment) %>").remove();
//-------------^

但是您的ERB这样说:

But your ERB says this:

<%= div_for comment do %>

您的 div_for 应该产生HTML是这样的:

Your div_for should produce HTML something like this:

<div id="comment_11">
    <!-- ... -->
<div>

假设comment是ID为11的Comment对象,则 dom_id 在相同的注释上将产生comment_11,这一切都很好.

Assuming that comment is a Comment object with id 11. Then dom_id on the same comment would produce comment_11 and that's all good.

但是,您正在使用@comment(一个实例变量)来生成jQuery的DOM ID 调用,但HTML的局部变量comment.我猜想comment是正确的,而您正在向dom_id提供nil,结果是ID不匹配,并且您的jQuery没有任何用处.

However, you're using @comment (an instance variable) to generate the DOM ID for the jQuery remove call but the local variable comment for the HTML. I'd guess that comment is the right one and you're feeding a nil to dom_id, the result is that the IDs don't match and your jQuery does nothing useful.

这篇关于javascript(jquery-rails)在rails 3.1.rc5中无法完全正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:30