0

1.我想做什么。

我想在他或她的时间轴上显示用户已加入的群组中发布的消息。因为这包括其他用户发布的消息,只要他们是他或她的群组成员。

2.我做了什么。

(show.html.erb-members)

<h1>Mypage</h1>
 <div id="timeline">
  <%= @timelines.each do |tl|%>
   <div class="message_area">
   <p class= "member_name"><a href="/members/<%= tl.member %>">
   <%= tl.member.name %></a></p>
   <p class="message_content"><%= tl.content %></p>
   <p class="message_create">(<%= tl.created_at.strftime'%Y-%m-%d %H:%M' %>)
   </p></div>
<% end %>
</div>

(members.controller.erb)

def show
  if !checklogin? then return end
   @me = me?
   @member = Member.find(params[:id])
   @timelines = []
   @timelines = @member.groups.each{|group| group.group_messages}

(成员.rb)

class Member < ActiveRecord::Base
 attr_accessible :admin, :mail, :memo, :name, :pass, :user

 has_many :friends
 has_many :messages
 has_many :comments
 has_many :group_in_members, :dependent => :destroy
 has_many :groups, :through => :group_in_members
end

(组.rb)

class Group < ActiveRecord::Base
 attr_accessible :admin, :memo, :name
 has_many :group_in_members, :dependent => :destroy
 has_many :members, :through => :group_in_members
 has_many :group_messages
end

(group_message.rb)

class GroupMessage < ActiveRecord::Base
 attr_accessible :content, :member_id, :group_id
  belongs_to :member
  belongs_to :group
end

3.错误

NoMethodError in Members#show

Showing /Users/******/******/****/app/views/members/show.html.erb    where line #32 raised:

undefined method `member' for #<Group:0x007fac639e2c00>
Extracted source (around line #32):

29: <div id="timeline"> 
30:  <%= @timelines.each do |tl|%>
31:   <div class="message_area">
32:   <p class= "member_name"><a href="/members/<%= tl.member %>">
33:    <%= tl.member.name %></a></p>
34:   <p class="message_content"><%= tl.content %></p>
35:   <p class="message_create">(<%= tl.created_at.strftime'%Y-%m-%d %H:%M' %>)
4

1 回答 1

0

这是经典!

你用过each你想用的地方map

@timelines = @member.groups.each{|group| group.group_messages}

groups 数组也是如此@timelines,因为这是每个数组的返回值,它只返回自己。

如果您知道如何正确调试,这对您来说是显而易见的。

一个简单的事情是使用<%= debug tl %>或使用inspect来找出您正在尝试使用的当前对象是什么。

于 2013-10-06T13:59:07.217 回答