Show username when posting comments in Rails

后端 未结 2 1844
隐瞒了意图╮
隐瞒了意图╮ 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 %>
    
    0 讨论(0)
  • 2021-01-29 01:03

    You save the username in

    @comment.username = current_user
    

    and show it with:

    <%= comment.user.name %>
    

    but it have to be

    <%= comment.username %>
    
    0 讨论(0)
提交回复
热议问题