2014-09-10 27 views
2

我想將評論表單添加到博客文章中,並且也顯示歷史註釋。我創建了兩個部分。我可以在posts/show視圖中看到評論表單,並且我可以留下評論,並將評論添加到數據庫中,但這些評論是 不顯示無法獲取歷史註釋以在視圖文件中顯示

我如何獲得這些歷史評論?

這裏是我的相關文件/摘錄:

應用程序/視圖/職位/ show.html.erb

<%= render partial: '/comments/comment', locals: { comments: @comments} %> 

<%= render partial: '/comments/form', locals: { comment: @comment } %> 

應用程序/意見/評論/ _comment.html.erb

<% comments.each do |comment| %> 
    <%= comment.body + " " + comment.user.name %> 
<% end %> 

app/views/comments/_form.html.erb

<%= form_for [@post, comment] do |f| %> 
    <%= f.label :body %> 
    <%= f.text_area :body %> 

    <%= f.submit "Save" %> 
<% end %> 

應用程序/控制器/ posts_controller.rb

def show 
    @topic = Topic.find(params[:topic_id]) 
    @post = Post.find(params[:id]) 
    @comments = @post.comments 
    @comment = Comment.new 
end 

應用程序/控制器/ comments_controller.rb

def create 
    @post = Post.find(params[:post_id]) 
    @topic = @post.topic 
    @comment = current_user.comments.new(comment_params) 
    @comment.post = @post 
    if @comment.save 
    redirect_to [@topic, @post], notice: "Comment was saved successfully." 
    else 
    flash[:error] = "Error creating comment. Please try again." 
    render :new 
    end 
end 

感謝。

回答

0

至於我可以看到,如果你確定是否被正確創建的意見(與右門柱相關),則有兩種主要的可能性:

  1. 有一些毛病你正在渲染局部的方式。該公約是取消領先/,我認爲這可能導致錯誤的路徑(所以comments/comment)。也就是說,我認爲如果沒有找到文件就會引發錯誤,而不是保持沉默。

  2. 有一些樣式或條件邏輯隱藏部分。它周圍是否有條件可能是錯誤的?任何原因都是display: none

變量@comments 可以被錯誤實例,但看起來合理的我。您可以測試出只需添加這行來顯示視圖的頂部,看到打印內容:

<%= @comments %>

如果評論一堆印刷,然後我會採取這一行了,並看看在部分內部添加<%= comments %>會發生什麼。縮小問題的範圍,直到你看到它的位置 - 因爲當我查看它時,上面的代碼看起來很好。

相關問題