2012-04-12 51 views
0

我想爲剛發佈的帖子獲取「.comment_container」,並將其放在最後一條評論之後。如何在ajax表單提交成功後獲取評論類,然後在頁面刷新時顯示它?

每個評論及其相關內容存儲在「.comment_container」類中。

下面的代碼做我需要但不是100%。而不是附加單詞測試我想附加新的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,第三評論3和第四評論4。 1,2,3和4的結果我得到的是這樣的:

Posting 1, 2 , 3 4

我有一種感覺它的東西做某種復位需要每個註釋離開之後做的。按預期刷新評論顯示之後。 1,2,3,4。有什麼想法?

親切的問候。

回答

1

你應該使局部渲染你的Comment(我想它應該是/views/comments/_comment.html.erb)。

然後只需更換:

.after("TEST"); 

有:

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

試過第一次和我的變量不會通過通過。 – LondonGuy 2012-04-12 11:25:34

+0

現在我看到,如果我可以將一些實例變量傳遞給來自我的comments_controllers創建方法的部分。 – LondonGuy 2012-04-12 11:31:36

+0

當然可以! 「創建」操作沒有辦法不知道當前的「評論」。只要將它分配給'@ comment'變量和你的'create.js.erb'(我想這就是你放置了被定義的JavaScript的地方)將能夠訪問它。此外,它將在平民中受歡迎。 – jdoe 2012-04-12 11:37:25

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

}); 
+0

什麼是「睡眠1」? – 2012-04-12 14:17:49

+0

按下提交按鈕後,就像1秒計數 – LondonGuy 2012-04-12 14:23:45

+0

但是爲什麼?我不明白你爲什麼要故意爲你的應用程序添加延遲。 – 2012-04-12 14:25:45