1

I am trying to create a feed that displays all comments for a model.

_comments.html.erb

<% if @comments.any? %>
    <ol>
      <%= render partial: 'shared/comment_feed', collection: @comments %>
    </ol>
    <%= will_paginate @comments %>
<% else %>
    <h2>Be the first one to comment on this episode!</h2>
<% end %>

_comment_feed.html.erb

<li id="<%= comment.id %>">
  <%= link_to gravatar_for(comment.user), comment.user %>
  <span class="user">
    <%= link_to comment.user.name, comment.user %>
  </span>
  <span class="content"><%= simple_format comment.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(comment.created_at) %> ago.
  </span>
  <% if current_user?(comment.user) %>
    <%= link_to "delete", comment, method: :delete,
                                   confirm: "Are you sure?",
                                   title: comment.content %>
  <% end %> 
</li>

The above code gives me an undefined local variable or method `comment' error. Doesn't rails automatically generate comment when a partial is used to render comments collections? Any help would be greatly appreciated.

1 Answer 1

5

The name of the local variable corresponds to the name of the partial. In your case the local variable is going to be named comment_feed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.