Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

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

General • Asked by Wesly Mezarina

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|
        = image_tag comment.user.avatar.url(:medium)
        = comment.user.username
           = comment.created_at.strftime("%b, %d")
          = comment.body

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

I get this error

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

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.

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

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.