2012-03-25 93 views
0

當我在Feed中創建評論時,rails會重複以前的所有評論。Rails重複評論問題

評論位指示

def create 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.create(params[:comment]) 
    redirect_to post_path(@post) 
end 

帖子顯示

<div class="content"> 
<%= markdown(@post.content) %> 

<ul class="comments"> 
<% @post.comments.each do |comment| %> 
    <%= render @post.comments %> 
<% end %> 
</ul> 

<%= render "comments/form" %> 
</div> 

_comment.html.erb

<li> 
<%= link_to comment.name, comment.url, :title => "visit website", :target => "_blank", :rel => "nofollow" %> 
<p><%= comment.body %></p> 
<time><%= comment.created_at.utc.strftime("%m.%d.%Y") %></time> 

<%= link_to 'X', [comment.post, comment], 
       :confirm => 'Are you sure?', 
       :method => :delete %> 
</li> 

Post.rb

has_many :comments, :dependent => :destroy 

Comment.rb

belongs_to :post 

後,我創建的第一個評論 First Comment

後,我創建的第一個評論 Second Comment

+0

'複製以前的所有comments.'? – Dogbert 2012-03-25 14:49:57

+0

請用一些當前的樣本輸出更清楚地定義'重複'。現在看起來每次添加評論時,所有評論都會顯示一個新的空白表單,但我猜測這不是您所看到的內容? – 2012-03-25 14:51:27

+0

我發表評論,它顯示正常。我創建另一個評論,它顯示在以前的評論下,然後複製他們下面的第一個和第二個評論。如果我提出第三條評論,則顯示在前兩個下面,並且在重複的兩條下面,然後再次重複三條評論。對不起,很難解釋。 – colmtuite 2012-03-25 14:53:33

回答

4

從代碼:

<% @post.comments.each do |comment| %> 
    <%= render @post.comments %> 
<% end %> 

這應該不包括:

<%= render @post.comments %> 

或:

<% @post.comments.each do |comment| %> 
    <%= render comment %> 
<% end %> 
2

我中有你你的循環需要怎樣的一種感覺是這樣的:

<ul class="comments"> 
<% @post.comments.each do |comment| %> 
    <%= render comment %> 
<% end %> 
</ul> 

而不是渲染,整個集合@post.comments