Save 36% for Black Friday! Learn more

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I make comments show the user who created it?

Wes asked in General

I would think that i could do something like the code below but it's not working. How can i approach this so it can show the user who created the comment and other information about the user? I did follow the tutorial video on polymorphic associations if that helps.

 - commentable.comments.each do |comment|
   .col-md-1
     .image 
        = image_tag comment.user.avatar.url(:medium)
   .col-md-11
     .username
        = comment.user.username
        .created_at
           = comment.created_at.strftime("%b, %d")
     .comment-body
        %p
          = comment.body
Reply

You can do exactly what you've got here with the association on the comments. What's not working?

Reply

I get this error

 undefined method `user' for #<Comment:0x007fc94a46b340>
Reply

Ah hmm, I can't remember, did that episode include adding a user_id column and belongs_to :user association to the comments? I thought it did but it's been a while. :)

If you don't have that, you'll need to do that to reference the user and that should do the trick.

Reply

I'm not sure how i missed that simple mistake. Thanks!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.