comment.user 返回的是 #<User:0x007f424ecb36c0> 而不是实际可读的用户



我有_comments.html。带有以下代码的动词

  <h3>Comments (<%= commentable.comments.count %>)</h3>
  <% commentable.comments.each do |comment| %>
  <%= comment.user %><br />
  <span class="timestamp">
  Posted <%= time_ago_in_words(comment.created_at) %> ago.
  <div class="well">
  <%= comment.body %>
  </div>
  <% end %>

和Commentscontroller

@comment = @commentable.comments.new comment_params
@comment.user = current_user
@comment.save
redirect_to @commentable, notice: "Your comment was successfully posted"

,

#<User:0x007f424ecb36c0> 
显示的是

而不是实际的用户名。如何才能正确显示用户名?我刚学完rails教程,对rails的所有东西都是新手。我正试着给微博加评论。工作到目前为止。只是不是用户信息。如有任何帮助,不胜感激

还有,当我在注释中添加name时。用户I get

NoMethodError in MicropostsController#show
undefined method `name' for nil:NilClass

你可以像这样获取用户的属性:

<%= comment.user.name %>

显示的是用户对象而不是用户名

<% comments = commentable.comments %>
 <h3>Comments (<%= comments.count %>)</h3>
    <% comments.each do |comment| %>
      <%= comment.user.name %><br />
      <span class="timestamp">
      Posted <%= time_ago_in_words(comment.created_at) %> ago.
      <div class="well">
      <%= comment.body %>
      </div>
    <% end %>

最新更新