为用户显示微博的评论

时间:2013-04-19 19:13:00

标签: ruby-on-rails

Micropost's comments on users page (Ruby on Rails)的基础上,我能够为每个微博添加新评论。但是,我在同一页面上显示每个帖子的评论时遇到问题。下面的代码没有抛出任何错误,所以我认为每个微博的评论都没有正确分配,因此无法显示。

用户> Microposts > 评论

users_controller.rb

  def show
    @user = Account.find(params[:id])
    @microposts = @user.microposts
    @micropost = Micropost.new
    @comments = @micropost.comments
    @comment = Comment.new
  end

微柱/ _micropost.html.erb

<li>
  <span class="content"><%= micropost.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
  </span>

  <span class="comments">
    <% if micropost.comments.any? %>
    <ul class="comments">
      <%= render @comments%>
    </ul>
    <% end %>

    <%= render "shared/form_comment", micropost: micropost %>
  </span>

</li>

评论/ _comment.html.erb

<li>
  <span class="content"><%= comment.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(comment.created_at) %> ago.
  </span>
</li>

2 个答案:

答案 0 :(得分:0)

<li>
  <span class="content"><%= micropost.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
  </span>

  <span class="comments">
    <ul class="comments">
      <%= render micropost.comments %> # you can leave out the #any?
    </ul>
   </span>

</li>

答案 1 :(得分:0)

<%= render @comments%>更改为<%= render micropost.comments %>以循环播放特定帖子的评论,而不是使用通用评论循环。