jeudi 22 février 2018

i ask a questions but every one say worng ans ,if i say that people down vote me pls pls help me with your ans in rails [duplicate]

This question already has an answer here:

I'm having trouble understanding to show user created post and comment pls help me stackoverflow ans

i geting ans but in ans it show all user data,but i wnt to display only user belongs to post and comment data,i don't kown people are down vote for this but it ans show all user data

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
  @post =  User.posts.all
  @comment = User.comments.all
end

in views index.html.erb

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

Aucun commentaire:

Enregistrer un commentaire