基于Micropost 在用户页面 (Ruby on Rails) 上的评论,我能够为每个微博添加新评论。但是,我无法在同一页面上显示每个帖子的评论。下面的代码没有抛出任何错误,所以我认为每个微博的评论没有正确分配,因此无法显示。
用户>微博>评论
users_controller.rb
def show
@user = Account.find(params[:id])
@microposts = @user.microposts
@micropost = Micropost.new
@comments = @micropost.comments
@comment = Comment.new
end
microposts/_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>