gem devise how to add comment created user email?

后端 未结 2 1393
[愿得一人]
[愿得一人] 2020-12-21 20:16

I am using gem devise for creating users profile Each user can create a comment. I need to add the user name beside each comment something like this <%= @comment.us

2条回答
  •  粉色の甜心
    2020-12-21 21:17

    You could do it the other way around, in your show method:

    @comments = Comment.all

    in your show view:

    <% @comments.each do |comment| %>
      <%= comment.text %>
      <%= comment.user.name %>
    <% end %>
    

    Since your question is not really clear I'll specifiy that if you want to show just the comments posted by the user:

    def show
      user_id = User.find(params[:id]).id
      @comments = Comment.where(user_id: user_id)
    end
    

提交回复
热议问题