2011-08-23 28 views
3

我目前正在使用Rails 2.3.5。我有一個模型博客和博客has_many評論。在我的意見顯示評論使用部分從另一個控制器的問題

  views/comments/_comment.html.erb 
    <li class="depth-1"> 
     <div class="comment-info"> 
      <cite> 
       <a href="index.html"><%= comment.user.name %></a> Says: <br> 
       <span class="comment-data"><a title="" href="#"><%= comment.created_at.stftime('%B %d, %Y at %I:%M %p')%></a></span> 
      </cite> 
     </div> 

     <div class="comment-text"> 
      <p><%= comment.content %></p> 

      <div class="reply"> 
       <a href="index.html" class="comment-reply-link" rel="nofollow">Comment</a> 
      </div> 
     </div> 
    </li> 

這部分是從blogs/show.html.erb和評論/ create.rjs調用。該部分從放映視圖這樣

<%= render :partial=>'comments/comment', @collection => @blog.comments %> 

調用,但是當我進入我的鏈接本地主機:3000 /博客/ 6,我得到以下錯誤

NoMethodError in Blogs#show 
    Showing app/views/comments/_comment.html.erb where line #6 raised: 
    undefined method `name' for nil:NilClass 
    Extracted source (around line #6): 

    3: 
    4: <div class="comment-info"> 
    5:  <cite> 
    6:  <a href="index.html"><%= comment.user.name %></a> Says: <br> 
    7:  <span class="comment-data"><a title="" href="#comment-63"><%= comment.created_at.stftime('%B %d, %Y at %I:%M %p')%></a></span> 
    8:  </cite> 
    9: </div> 

    Trace of template inclusion: app/views/blogs/show.html.erb 

但用戶屬性是明確界定並且該名稱對所有用戶都存在。

+0

什麼是用戶和評論 –

+0

用戶的has_many的意見和評論之間的關係belongs_to的用戶 – Rahul

+0

同時節省您的連接的用戶的ID發表評論或沒有回覆r? –

回答

2

您輸入@代替冒號:在您的部分,請檢查Partials documentation

的正確格式爲:

<%= render :partial=>'comments/comment', :collection => @blog.comments %> 
+0

Thanx ...這是如此愚蠢,如果我.. [尷尬..] – Rahul

+1

不要發生:) –

相關問題