繁体   English   中英

基于AJAX的评论不会发布正确的微博(Ruby on Rails)

[英]AJAX based comments don't post under correct micropost (Ruby on Rails)

在用户的页面上有微博。 他们每个人都有评论表。 评论由AJAX发布。 创建后,评论必须出现在使用评论表格的微博下,但由于某种原因,评论总是在最后一个微博下发布。

在DB中有一切正常 - 在创建评论后,我有下一个正确的信息: micropost_id,user_id,comment_id。 所以刷新页面后所有评论都在正确的位置。

如果没有刷新,我应该怎样做才能在正确的微博下发布评论?

comment.rb

class Comment < ActiveRecord::Base
  attr_accessible :comment_content
  belongs_to :user
  belongs_to :micropost
end

comments_controller.rb

class CommentsController < ApplicationController
  before_filter :signed_in_user, only: [:create, :destroy]

   def create
    @micropost = Micropost.find(params[:micropost_id])
    @comment = Comment.new(params[:comment])
    @comment.micropost = @micropost
    @comment.user = current_user
      respond_to do |format|
      @comment.save
           format.html { redirect_to current_user }
           format.js
      end
   end 
end

_micropost.html.erb

<tr>
  <td class="micropost">
    <span class="content"><%= wrap(micropost.content) %></span>
    <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
    </span>
    <%= render 'shared/comment_form', micropost: micropost %>
   <div id="comments">
     <%= render micropost.comments %>
   </div>
  </td>
</tr>

_comment_form.html.erb

<%= form_for ([micropost, @comment]), :remote => true do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :comment_content, :size => "40x2" %>
  <button class="btn" type="submit">
    Comment
  </button>
<% end %>

_comment.html.erb

<span style="width: 100%; background:#dff0d8"><%= wrap(comment.comment_content) %></span>
<span class="timestamp">
 Posted by <%= comment.user.name %> <%= time_ago_in_words(comment.created_at) %> ago.
</span>

create.js.erb

$('#comments').html("<%= escape_javascript(render(:partial => @micropost.comments)) %>");

首先,不要多次使用ID( #comments )。 ID应该在整个页面上是唯一的。 我尝试更改_micropost.html.erb以使用:

<div id="<%= dom_id(micropost) %>">
  <%= render micropost.comments %>
</div>

然后将create.js.erb更改为

$('#<%= dom_id(@micropost) %>').html("<%= escape_javascript(render(:partial => @micropost.comments)) %>");

create.js.erb文件中尝试以下代码:

$('#comments_<%=@micropost.id%>:last').append("<div><span style='width: 100%; background:#dff0d8; font-size: 12pt' ><%= wrap(@comment.comment_content) %></span><br> <span class='timestamp' style='width: 100%; background:#dff0d8; font-size: 10pt'> Posted by<%= @comment.user.name %> <%= time_ago_in_words(@comment.created_at) %> ago.</span> </div>")

看到你知道发生了什么,评论只是对不同微博的所有评论的ID

因此浏览器不知道在第一个微博或最后一个评论中添加评论的位置,因此您必须为div评论创建动态ID。 这就是我在上面的例子中所做的。

另外,尝试在firefox中运行代码并使用firebug进行检查。

尝试使用create.js.erb

$(this).parent().find('#comments').html("<%= escape_javascript(render(:partial => @micropost.comments)) %>");

或者甚至更好,只是添加新评论

$(this).parent().find('#comments').prepend("<%= escape_javascript(render 'comments/comment', :comment => @comment) %>");
$(this).parent().find('#comments').prepend("<%= escape_javascript(render 'comments/comment', :comment => @comment) %>");

当你这样做时,没有褪色或淡出效果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM