ex:i have four users: 1)userx,2)usery,3)userA,4)userB
in 4 users i want to display only userA post and comment, I'm having trouble understanding how to show a single user's created post and comments.
post model
belongs_to :user
has_many :comments
comment model
belongs_to :post
belongs_to :user
user model
has_many :posts
has_many :comments
routes.rb
resources :posts do
resources :comments
end
in controllers
def index
@users = User.includes(:posts, :comments)
end
in your views:
Views #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 %>
Views #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 %>
i check with this code, but it show all user data i want to show perticular user date pls help me
Aucun commentaire:
Enregistrer un commentaire