2015-06-28 74 views
-1

這顯示所有的評論,但它也顯示了最後一個額外的編輯和刪除按鈕。但是,當我顛倒這個順序時,我顯示添加評論後,所有評論這個錯誤似乎消失。任何人都可以指出發生了什麼?另外,我希望提出更好的編碼標準。迭代的軌道窗體顯示額外的按鈕

<div class="alert alert-success"> 
<h3>Add Comment</h3> 
<%= form_for([@article, @article.comments.build]) do |f| %> 
<%= f.text_field :name, size:80 %> 
<%= f.submit "Comment", class: "btn btn-primary" %> 
<% end %> 
</div> 

<h3>Comments</h3> 

<% @article.comments.each do |comment| %> 
<div class="alert alert-info"> 
    <h4><%= comment.name %></h4> 
    <table> 
     <tr> 
      <td><%= link_to 'Edit', [comment.article, comment], method: :put, class: "btn btn-primary" %></td> 
      <td><%= button_to 'Delete', [comment.article, comment], method: :delete, class: "btn btn-danger" %></td> 
     </tr> 
    </table> 
</div> 
<% end %> 
+0

根據您的代碼,行爲是正確的。你想要它做什麼? – Pavan

+0

爲什麼在顯示所有評論後會顯示額外的編輯和刪除按鈕? –

+0

由於您在代碼中包含它們,因此它會顯示:) – Pavan

回答

0

你必須調整這樣

<table> 

    <% @articles.each do |article| %> 
    <div class="alert alert-info"> 
    <tr> 
     <td><h4><%= comment.name %></h4></td> 
     <td><%= link_to 'Edit', [comment.article, comment], method: :put, class: "btn btn-primary" %></td> 
     <td><%= link_to 'Delete', [comment.article, comment], method: :delete, class: "btn btn-danger" %></td> 
    </tr> 
</div> 
<% end %> 

</table> 

你的代碼還可以使用link_tobutton_to