简体   繁体   English

如何在成功提交Ajax表单后获取注释类,然后显示它并进行页面外刷新?

[英]How do I grab a comments class after a successful ajax form submit then display it with out page refresh?

I want to get ".comment_container" for the post just made and place it after the last comment. 我想获取刚刚发布的帖子的“ .comment_container”,并将其放在最后的评论之后。

Each comment and it's related content is stored in a ".comment_container" class. 每个评论及其相关内容都存储在“ .comment_container”类中。

The code below does what I need but not 100%. 下面的代码可以满足我的需要,但不能100%完成。 Rather than append the word TEST I want to append the new comment_contaner holding the comment just posted. 而不是附加单词TEST,我想附加新的comment_contaner,其中包含刚刚发布的评论。

I've been cracking at this all day and this is how far I've come. 我整天都在努力,这就是我走的路。 I would appreciate some solutions with examples if possible. 如果可能的话,我希望一些解决方案提供示例。

JQuery: jQuery的:

$('#new_comment').on('ajax:success', function(){
  $(this).parent('.post_content').find('.comment_container:last').after("TEST");
});

<% sleep 1 %>

HTML: 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>

Comments controller: 评论控制器:

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

Comment partial: 评论部分:

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

Seems to be working apart from 1 minor issue. 似乎正在解决1个小问题。 Let's say I post 4 comment... 1 after each other. 假设我发表了4条评论...彼此之间是1条。 First comment 1, second 2, third 3 and fourth 4.. eg 1, 2, 3 and 4 the result I get is this: 第一个注释1,第二个2,第三个3和第四个4 ..例如1、2、3和4,我得到的结果是:

发表1、2、3 4

I have a feeling it's something to do with some kind of reset needing to be done after each comment is left. 我感觉这与在每次评论后都需要进行某种重置有关。 After refreshing the comments display as expected. 刷新后,注释将按预期显示。 1, 2, 3 and 4. Any thoughts? 1、2、3和4。有什么想法吗?

Kind regards. 亲切的问候。

You should make the partial for rendering your Comment s (I guess it should be the /views/comments/_comment.html.erb ). 您应该将部分内容用于呈现Comment (我想应该是/views/comments/_comment.html.erb )。

Then just replace: 然后只需替换:

.after("TEST");

with: 与:

.after("<%= j render @comment %>");

This along with the advice from jdoe fixed my issue 这与jdoe的建议一起解决了我的问题

$('.new_comment').off().on('ajax:success', function(e){
 e.preventDefault();
    $(this).parent('.post_content').find('.comment_container:last').after("<%= j render 'comments/partials/comment' %>");
    $('#comment_content').removeClass("comment_box_focused").addClass("comment_box");
    $(".commentButtons").removeClass("displayButtons");
    $('#comment_content').val("");
    <% sleep 1 %>

});

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

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