我想在group-message-comments旁边显示用户名。但是我在“<%= comment.member.name%>”周围有一条错误消息在show.html.erb上。我怎么解决这个问题? 我不明白为什么会发生这种错误,因为我把“belongs_to”放在group_message_comment.rb上,而会员模型有“name”列。
☆show.html.erb(group_messages controller)
<p><b>Comments:</b></p>
<ul>
<% if @group_message.group_message_comments.any? %>
<% @group_message.group_message_comments.each do |comment| %>
<li>
<%= comment.content %> (by <%= comment.member.name %>)
</li>
<% end %>
<% else %>
<li>no comment yet.</li>
<% end %>
</ul>
☆member.rb
has_many :friends
has_many :messages
has_many :comments
has_many :group_in_members, :dependent => :destroy
has_many :groups, :through => :group_in_members
has_many :group_message_comments
has_many :group_messages
☆group_message_comment.rb
belongs_to :member
belongs_to :group_message
答案 0 :(得分:1)
当没有特定评论的成员时会发生这种情况。
在此处添加条件:
(by <%= comment.member ? comment.member.name : "Unknown" %>)