1

モデルのすべてのコメントを表示するフィードを作成しようとしています。

_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>

上記のコードは、未定義のローカル変数またはメソッドの「コメント」エラーを表示します。パーシャルを使用してコメントコレクションをレンダリングすると、railsは自動的にコメントを生成しませんか?どんな助けでも大歓迎です。

4

1 に答える 1

5

ローカル変数の名前は、パーシャルの名前に対応しています。あなたの場合、ローカル変数には。という名前が付けられcomment_feedます。

于 2013-03-01T02:23:08.947 に答える