我正在为我的应用构建一个消息传递系统,这几乎就是Stackoverflow的工作原理。我有两个循环运行。一个用于每个帖子,然后在里面,一个用于评论该帖子。马上;它显示整个数据库中的所有注释,而不仅仅是那些帖子。
discussion.html.erb
<% @projects.posts.each do |post| %>
<%= post.content %>
</div>
<% @projects.posts.comments.each do |comment| %>
<%= comment.content %>
</div>
<% end %>
<% end %>
projects_controller.rb
def discussion
@projects = Project.includes(:posts => :comments).find(params[:id])
@posts = Project.find(params[:id]).posts
@comments = Project.find(params[:id]).posts.comments
end
答案 0 :(得分:1)
<% @projects.posts.each do |post| %>
<div> <!-- Need to open your div -->
<%= post.content %>
</div>
<% post.comments.each do |comment| # Scope to the current post %>
<div> <!-- Need to open your div -->
<%= comment.content %>
</div>
<% end %>
<% end %>