简体   繁体   English

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

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

On the user's page there are microposts. 在用户的页面上有微博。 Each of them have comment form. 他们每个人都有评论表。 Comments are posting by AJAX. 评论由AJAX发布。 After creating, comment must appear under micropost which comment form was used, but for some reason comment always posts under the last micropost. 创建后,评论必须出现在使用评论表格的微博下,但由于某种原因,评论总是在最后一个微博下发布。

In DB there is all OK - after creating comment i have there next correct information: micropost_id, user_id, comment_id. 在DB中有一切正常 - 在创建评论后,我有下一个正确的信息: micropost_id,user_id,comment_id。 So after refreshing page all comments are on the correct places. 所以刷新页面后所有评论都在正确的位置。

What should i do to have comments posted under correct micropost without refreshing? 如果没有刷新,我应该怎样做才能在正确的微博下发布评论?

comment.rb comment.rb

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

comments_controller.rb 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 _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 _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 _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 create.js.erb

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

First off, don't use an ID ( #comments ) multiple times. 首先,不要多次使用ID( #comments )。 An ID is supposed to be unique on the entire page. ID应该在整个页面上是唯一的。 I'd try changing _micropost.html.erb to use: 我尝试更改_micropost.html.erb以使用:

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

and then change create.js.erb to 然后将create.js.erb更改为

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

Try the following code in your create.js.erb file: 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>")

see you know what is happening , comments is nothing but an id for all comments for different microposts 看到你知道发生了什么,评论只是对不同微博的所有评论的ID

so browser does not know where to add comment whether in comments of first micropost or last so you have to create dynamic id for div comments. 因此浏览器不知道在第一个微博或最后一个评论中添加评论的位置,因此您必须为div评论创建动态ID。 That is what I have done in the above example. 这就是我在上面的例子中所做的。

Also, try running your code in firefox and inspect it using firebug. 另外,尝试在firefox中运行代码并使用firebug进行检查。

Try in create.js.erb 尝试使用create.js.erb

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

Or even better, just prepend the new comment 或者甚至更好,只是添加新评论

$(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