我想为刚刚发布的帖子获取“.comment_container”并将其放在最后一条评论之后。
每个评论及其相关内容都存储在“.comment_container”类中。
下面的代码可以满足我的需要,但不是 100%。我不想附加 TEST 这个词,而是想附加新的 comment_contaner 来保存刚刚发布的评论。
我整天都在努力解决这个问题,这就是我已经走了多远。如果可能的话,我将不胜感激一些带有示例的解决方案。
查询:
$('#new_comment').on('ajax:success', function(){
$(this).parent('.post_content').find('.comment_container:last').after("TEST");
});
<% sleep 1 %>
HTML:
<div class="postHolder">
<nav class="micropostOptions">
<ul class="postMenu">
<li class="deletePost"><%= link_to content_tag(:span, "Delete post"), m, :method => :delete, :confirm => "Are you sure?", :title => m.content, :class => "message_delete" %>
</li>
<li class="disableCommenting"><%= link_to content_tag(:span, "Pause commenting"), "2" %></li>
<li class="blockCommenter"><%= link_to content_tag(:span, "Block commenter"), "3" %></li>
<li class="openInNewWindow"><%= link_to content_tag(:span, "Open in new window"), "4" %></li>
<li class="reportAbuse"><%= link_to content_tag(:span, "Report abuse"), "5" %></li>
</ul>
</nav>
<%= link_to image_tag(default_photo_for_current_user, :class => "poster_photo"), current_users_username %>
<div class="post_content">
<div class="post_container">
<div class="userNameFontStyle"><%= link_to current_users_username.capitalize, current_users_username %> -
<div class="post_time"> <%= time_ago_in_words(m.created_at) %> ago.</div> </div>
<%= simple_format h(m.content) %> </div>
<% if m.comments.any? %>
<% comments(m.id).each do |comment| %>
<div class="comment_container">
<%= link_to image_tag(default_photo_for_commenter(comment), :class => "commenter_photo"), commenter(comment.user_id).username %>
<div class="commenter_content"> <div class="userNameFontStyle"><%= link_to commenter(comment.user_id).username.capitalize, commenter(comment.user_id).username %> - <%= simple_format h(comment.content) %> </div>
</div><div class="comment_post_time"> <%= time_ago_in_words(comment.created_at) %> ago. </div>
</div>
<% end %>
<% end %>
<% if logged_in? %>
<%= form_for @comment, :remote => true, :html => {:class => "new_comment} do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :micropost_id, :value => m.id %>
<%= f.text_area :content, :placeholder => 'Post a comment...', :class => "comment_box", :rows => 0, :columns => 0 %>
<div class="commentButtons">
<%= f.submit 'Post it', :class => "commentButton" %>
<div class="cancelButton"> Cancel </div>
</div>
<% end %>
<% end %>
</div>
</div>
评论控制器:
class CommentsController < ApplicationController
def create
@comment = Micropost.find(params[:comment][:micropost_id]).comments.build(params[:comment])
respond_to do |format|
if @comment.save
unless params[:comment][:recipient].blank? # this will be blank when current user is commenting/replying on their own wall
recipient = User.find(params[:comment][:recipient])
UserMailer.new_wall_post_comment_notification(recipient, current_user).deliver if recipient.email_notification == 1
end
format.js { render :post_comment }
else
format.js { render :form_errors }
end
end
end
end
部分评论:
<div class="comment_container">
<%= link_to image_tag(default_photo_for_commenter(@comment), :class => "commenter_photo"), commenter(@comment.user_id).username %>
<div class="commenter_content">
<div class="userNameFontStyle"><%= link_to commenter(@comment.user_id).username.capitalize, commenter(@comment.user_id).username %> - <%= simple_format h(@comment.content) %>
</div>
</div>
<div class="comment_post_time">
<%= time_ago_in_words(@comment.created_at) %> ago.
</div>
</div>
似乎除了 1 个小问题外还在工作。假设我发表了 4 条评论... 1 条。第一条评论 1、第二条 2、第三条 3 和第四条 4.. 例如 1、2、3 和 4 我得到的结果是这样的:
我有一种感觉,这与在留下每条评论后需要进行某种重置有关。刷新后评论按预期显示。1、2、3和4。有什么想法吗?
亲切的问候。