2014-12-03 67 views
1

我創建了評論系統來提供創建評論和回覆它們的功能。 我用http://www.sitepoint.com/nested-comments-rails/指南。工作完美。但在這個例子中,回覆一些評論時,它會轉到其他路徑,這就是我想要避免的。回覆在同一頁面中評論不在評論/新建路徑,Rails 4

目前代碼: Advertisement#show這裏我想創建回覆評論。

<%= comments_tree_for @comments %> 

<h1>New comment</h1> 
<%= render 'comments/form' %> 

_comment.html.rb

<div class="well"> 
    <h2><%= comment.title %></h2> 
    <p class="text-muted"><%= comment.root? ? "Started by" : "Replied by" %> <strong><%= comment.author %></strong> on 
    <%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p> 


<blockquote> 
    <p><%= comment.body %></p> 
</blockquote> 

<% from_reply_form ||= nil %> 
<% unless from_reply_form %> 
    <% if comment.leaf? %> 
    <small class="text-muted">There are no replies yet - be the first one to reply!</small> 
    <% end %> 
    <p><%= link_to 'reply', new_comment_path(comment.id) %></p> 
<% end %> 
</div> 

_form.html.erb

<%= form_for(@comment) do |f| %> 
    <% if @comment.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2> 

     <ul> 
     <% @comment.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <%= f.hidden_field :advertisement_id, :value => @advertisement.id%> 
    <%= f.hidden_field :user_id, :value => current_user.id%> 
<%= f.hidden_field :parent_id %> 

    <div class="form-group"> 
    <%= f.label :body %> 
    <%= f.text_area :body, class: 'form-control', required: true %> 
    </div> 

    <%= f.submit class: 'btn btn-primary' %> 
<% end %> 

是否有值得信賴的指南,以幫助我嗎?

+1

只是要求你打的創建方法時您發表的帖子或者你是不是? – 2014-12-03 21:09:55

+0

@Aaditi Jain你的意思是當我創建評論?還是廣告? – Edgars 2014-12-04 10:33:45

回答

3

link_to 'reply', new_comment_path(comment.id)行創建了一個超鏈接到新評論頁面,點擊後它把你帶到下一個頁面來創建評論。相反,您可以將其替換爲包含切換形式的div,並從同一頁面提交新的convo。 Bootstrap在這裏很方便。

一個模擬了這個想法的:http://jsfiddle.net/bpya4fce/1/

您可以建立使用這種想法。希望這會有所幫助:)

PS:確保獲取嵌入表單的視圖的action中的表單所需的變量,即如果將表單嵌入到show.html.erb中,請確保獲取必要的變量在show控制器的動作/方法。在前面的場景中,它將在CommentsController的new操作中獲取。


<div class="container"> 
<blockquote> 
    <h2>Posted Comment</h2> 
    Lorem Ipsum. You can reply to this below. 
</blockquote> 

<div class="panel-group" id="accordion"> 
    <div class="panel panel-default"> 
    <div class="panel-heading"> 
     <h4 class="panel-title"> 
     <a data-toggle="collapse" data-parent="#accordion" 
      href="#collapseOne"> 
      Click to reply 
     </a> 
     </h4> 
    </div> 
    <div id="collapseOne" class="panel-collapse collapse"> 
     <div class="panel-body"> 
      ### The comment form comes here ###<br/> 
      ### render :partial => 'comment/form' 
     </div> 
    </div> 
    </div> 
</div> 

1

不知道這是你想要的。希望這個示例代碼給你一些想法。 一定要完成代碼和測試它:)

_comment.html.rb

<% from_reply_form ||= nil %> 
<% unless from_reply_form %> 
    <% if comment.leaf? %> 
    <small class="text-muted">There are no replies yet - be the first one to reply!</small> 
    <% end %> 
    <!-- HERE adding a hidden DIV that contains a form. --> 
    <div class='hidden-reply-form-<%= comment.id%>'> 
    <%= render partial: 'comments/form', locals: {comment: Comment.new} %> 
    </div> 
    <p><%= link_to 'reply', new_comment_path(comment.id), class: 'reply', id: comment.id %></p> 
<% end %> 
</div> 

<script> 
    // HERE use reply link to toggle the hidden div. 
    $(function(){ 
    $(".reply").click(function(){ 
      // toggle replying div. 
      // ..... 
    }); 
}) 
</script>