为什么这个Ajax请求更换新提交评论我所有的评论? [英] Why does this ajax request replace all my comments with the newly submitted comment?

查看:122
本文介绍了为什么这个Ajax请求更换新提交评论我所有的评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有这种形式,其要求时,注释提交了JS调用:

 <%= simple_form_for([@视频,@ video.comments.new]:远程=>真)并| F | %>
  <%= f.association:comment_title,:集合=> @ video.comment_titles,:标签=> 评论标题::include_blank =>假%>
  <%= f.input:身体:标签=>假:占位符=> 发表评论。 %>
  <%= f.button:提交:价值=> 邮报%GT;
<%结束%GT;
 

它调用此创建的意见控制器动作:

 高清创建
  @comment = @ video.comments.new(PARAMS [:发表评论] .merge({:USER_ID => current_user.id}))
  如果@ comment.save
    respond_to代码做|格式|
      的format.html {redirect_to时:回}
      format.js
    结束
  其他
    respond_to代码做|格式|
      的format.html {redirect_to时:回来了,:警惕=> 无法添加评论。 }
      format.js {渲染fail_create.js.erb'}
    结束
  结束
结束
 

这使得这个create.js.erb文件:

  $(。comment_content)HTML('<%= escape_javascript(渲染(@comment))%>');
$(。comment_form)[0] .reset段();
 

以上create.js.erb文件呈现的注释部分到该文件中:

 <%comment_title.comments.each办|评论| %>
     < D​​IV CLASS =comment_content>
        <%=呈现评论%>
     < / DIV>
<%结束%GT;
 

为什么我所有的意见被替换在此AJAX请求的新提交评论?

下面是我评论的部分:

 <%=的link_to image_tag(comment.user.profile.photo.url(:很小)),profile_path(comment.user.profile):类=> comment_image%>
< D​​IV CLASS =textual_comment_content>
    < D​​IV CLASS =COMMENT_TEXT>
        <跨度类=name_link>
            <%=的link_to#{comment.user.name},profile_path(comment.user.profile):类=> 正常%>
        < / SPAN>
        &其中;%= comment.body.gsub(',&安培;者;')。html_safe%GT;
    < / DIV>
    <跨度类=comment_footer>
        < UL>
            <李类=list_style><%= time_ago_in_words(comment.created_at)%>前< /李>
            <%,除非CURRENT_USER = comment.user%&GT!;
            <李><%=的link_to删除,video_comment_path(:VIDEO_ID => @video,:ID =>注释):方法=> :删除:类=> 正常的%>< /李>
            <%结束%GT;
        < / UL>
     < / SPAN>
 < / DIV>
 

解决方案

首先,您需要将来自第一code包装div来部分:

 < D​​IV CLASS =comment_content>
< / DIV>
 

然后就前插入HTML /首/最后一个元素之后:

  $(comment_content:第一)。前('<%= escape_javascript(渲染(@comment))%>');
$(。comment_content:最后一个)后('<%= escape_javascript(渲染(@comment))%>');
 

So I have this form which requests a JS call when a comment is submitted:

<%= simple_form_for([@video, @video.comments.new], :remote => true) do |f| %>
  <%= f.association :comment_title, :collection => @video.comment_titles, :label => "Comment Title:", :include_blank => false %>
  <%= f.input :body, :label => false, :placeholder => "Post a comment." %>
  <%= f.button :submit, :value => "Post" %>
<% end %>

It calls this create action in the comments controller:

def create
  @comment = @video.comments.new(params[:comment].merge({:user_id => current_user.id}))
  if @comment.save
    respond_to do |format|
      format.html { redirect_to :back }
      format.js
    end
  else
    respond_to do |format|
      format.html { redirect_to :back, :alert => "Unable to add comment." }
      format.js { render 'fail_create.js.erb' }
    end
  end
end

which renders this create.js.erb file:

$(".comment_content").html('<%= escape_javascript(render(@comment)) %>');
$(".comment_form")[0].reset();

The above create.js.erb file renders the comment partial into this file:

<% comment_title.comments.each do |comment| %>
     <div class="comment_content">
        <%= render comment %>
     </div>
<% end %>

Why are all of my comments being replaced by the newly submitted comment in this AJAX request?

Here's my comment partial:

<%= link_to image_tag(comment.user.profile.photo.url(:tiny)), profile_path(comment.user.profile), :class => "comment_image" %>
<div class="textual_comment_content">
    <div class="comment_text">
        <span class="name_link">
            <%= link_to "#{comment.user.name}", profile_path(comment.user.profile), :class => "normal" %>
        </span>
        <%= comment.body.gsub("'",'&apos;').html_safe %>
    </div>
    <span class="comment_footer">
        <ul>
            <li class="list_style"><%= time_ago_in_words(comment.created_at) %> ago</li>
            <% unless current_user != comment.user %>
            <li><%= link_to "Delete", video_comment_path(:video_id => @video, :id => comment), :method => :delete, :class => "normal" %></li>
            <% end %>
        </ul>
     </span>
 </div>

解决方案

first You need to move wrapping div from first code to the partial:

<div class="comment_content">
</div>

then just insert html before/after first/last element:

$(".comment_content:first").before('<%= escape_javascript(render(@comment)) %>');
$(".comment_content:last").after('<%= escape_javascript(render(@comment)) %>');

这篇关于为什么这个Ajax请求更换新提交评论我所有的评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆