使用来自另一个控制器的partial的问题

时间:2011-08-23 12:47:08

标签: ruby-on-rails

我目前正在研究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和comments / create.rjs。部分从show视图中调用

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

但是当我访问我的链接localhost:3000 / blogs / 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

但是用户属性定义良好,并且所有用户都存在该名称。

1 个答案:

答案 0 :(得分:2)

您在部分中输入了@而不是冒号:,请检查Partials documentation

正确的格式是:

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