0

作成したばかりの投稿の「.comment_container」を取得して、最後のコメントの後に配置したいと思います。

各コメントとそれに関連するコンテンツは、「。comment_container」クラスに保存されます。

以下のコードは私が必要とすることを実行しますが、100%ではありません。TESTという単語を追加するのではなく、投稿したばかりのコメントを保持する新しいcomment_contanerを追加します。

私はこれで一日中ひび割れていました、そしてこれは私がどこまで来たかです。可能であれば、例を挙げていくつかの解決策をいただければ幸いです。

JQuery:

$('#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番目の2、3番目の3、4番目の4。たとえば、1、2、3、4の結果は、次のようになります。

投稿1、2、3 4

コメントが残った後、何らかのリセットを行う必要があるのではないかと感じています。コメントを更新すると、期待どおりに表示されます。1、2、3、4。何か考えはありますか?

敬具。

4

2 に答える 2

1

をレンダリングするためにパーシャルを作成する必要がありますComment(おそらくそうあるべきです/views/comments/_comment.html.erb)。

次に、置き換えます。

.after("TEST");

と:

.after("<%= j render @comment %>");
于 2012-04-12T11:20:58.313 に答える
0

これは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 %>

});
于 2012-04-12T12:55:01.840 に答える