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

Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Wesly Mezarina 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

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!

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.