mercredi 21 février 2018

How to show user created post and comment in rails?

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

i getting error in this method

undefined method `posts' for User (call 'User.connection' to establish a connection):Class

 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