Show username when posting comments in Rails

后端 未结 2 1852
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 00:36

I have an app when users post album reviews called Pins. I created a comments model for other users to comment on the reviews. I\'m struggling getting the comments to say \"Post

2条回答
  •  攒了一身酷
    2021-01-29 00:58

    You are assigning a User instance to the username field for the Comment. I assume that the username attribute is a string in the database. So if you want the name to appear in the comment then you need to assign it the name from the current user.

    So:

    @comment.username = current_user.name
    

    If you already have an association between Comment and User then you could do:

    @comment.user = current_user
    
    <%= @comment.user.name %>
    

提交回复
热议问题