如何显示当前用户post和所有post的评论?

How to show the current user's post and the all posts comments?

我有很多用户,如何只列出一个post的用户,同时显示其他用户的所有评论?

post 型号

belongs_to :user
has_many :comments 

评论模型

belongs_to :post
belongs_to :user

用户模型

 has_many :posts
 has_many :comments

routes.rb

 resources :posts do
     resources :comments
 end

在控制器中

def index
    @post =  User.posts.all
    @comment = User.comments.all
end

观看次数index.html.erb

<%@post.each do |post|%>
    <%=post.post_name %>
    <%= post.post_description %> 
<%end%>

<%@comment.each do |comment|%>
    <%=comment.content %>
<%end%>

如何仅显示当前用户的 post 和评论?

选项 #1

你的用户模型 have_many :posts 和 Post has_many :comments 所以你可以在你的控制器中这样做:

def index
  @users = User.all
end

并由用户调用每个 post 并由 post 评论:

index.html.erb

<% @users.each do |user| %>
  <% user.posts.each do |post|%>
    <%= post.post_name %>
    <%= post.post_description %>
    <% post.comments.each do |comment| %>
      <%= comment.content %>
    <% end %>
  <% end %>
<% end %>

选项 #2 使用 includes:

def index
  @users = User.includes(:posts, :comments)
end

您认为:

观看次数 #1

<% @users.each do |user| %>
  <% user.posts.each do |post| %>
    <%= post.post_name %>
    <%= post.post_description %>
  <% end %>

  <% user.comments.each do |comment| %>
    <%= comment.content %>
  <% end %>
<% end %>

观看次数 #2:

<% @users.each do |user| %>
  <% user.posts.each do |post| %>
    <%= post.post_name %>
    <%= post.post_description %>
    <% post.comments.each do |comment| %>
      <%= comment.content %>
    <% end %>
  <% end %>
<% end %>

观看次数#3 我假设您的 User 模型中有 first_name 字段。

<% @users.each do |user| %>
  <%= user.first_name %> 

  <% user.posts.each do |post| %>
    <%= post.post_name %>
    <%= post.post_description %>

    <% post.comments.each do |comment| %>
      <%= comment.content %>
    <% end %>

  <% end %>
<% end %>

我会尽力帮助你,让我们先澄清你的问题,好吗?您只想显示 post 的创建用户,只向 post 的所有者显示 post 以及所有评论吗?还是要列出所有 post,并在 post 内列出评论?如果不是,你能举个例子吗? 拿Araratan举的例子来说,什么不符合你的要求? 你可以尝试这样的事情:

你的users_controller.rb

 def index
      @users = User.all
    end

你的posts_controller.rb

def index
   @posts= Post.all
end

你的comments_controller.rb

 def index
      @comments = Comment.all
  end

在您看来post。 你可以放这个。

<% current_user.posts.each |post| %>
    #post fields like
    # <%= post.title %>
    <% post.comments.each do |comment| %>
        #comments fields like 
        # <%= comment.description %>
        #do you wanna show the author of comment?
    <% end %>
    #or, do you wanna show the author of post?
<% end %>
#from what I understand you want to show both, that?

你可以使用 Araratan 答案,但不是列出所有用户和所有用户的所有 posts,你可以把当前用户。如果您已经实现了登录部分。 我会举个例子并适应

选项#1

你的用户模型 have_many :posts 和 Post has_many :comments 所以你可以在你的控制器中这样做:

def index
  @users = User.all
end

并由用户调用每个 post 并由 post 评论:

index.html.erb

  <% current_user.posts.each do |post|%>
    <%= post.post_name %>
    <%= post.post_description %>
    <% post.comments.each do |comment| %>
      <%= comment.content %>
    <% end %>
  <% end %>

选项#2 使用 includes:

def index
  @users = User.includes(:posts, :comments)
end

您认为:

观看次数 #1

#but I think that's not what you want

  <% current_user.posts.each do |post| %>
    <%= post.post_name %>
    <%= post.post_description %>
  <% end %>

  <% current_user.comments.each do |comment| %>
    <%= comment.content %>
  <% end %>

观看次数 #2:

#I think this is what you want


  <% current_user.posts.each do |post| %>
    <%= post.post_name %>
    <%= post.post_description %>
    <% post.comments.each do |comment| %>
      <%= comment.content %>
    <% end %>
  <% end %>