Rahul
Rahul

Reputation: 47096

issues with using partial from another controller

I am currently working on Rails 2.3.5. I have a model Blog and Blog has_many comments. In my comment for displaying the comment

            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>

this partial is called both from blogs/show.html.erb and comments/create.rjs. The partial is called from the show view as such

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

but when i access my link localhost:3000/blogs/6, i get the following error

    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

but the user attribute is well defined and the name exists for all users.

Upvotes: 3

Views: 138

Answers (1)

KARASZI Istv&#225;n
KARASZI Istv&#225;n

Reputation: 31467

You entered @ instead of colon : in your partial, check the Partials documentation.

The correct format is:

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

Upvotes: 2

Related Questions