在mypage上实现时间线

时间:2013-10-06 09:29:37

标签: ruby-on-rails model ruby-on-rails-3.2

1.我想做什么。

我想在他或她的时间线上显示用户已加入的组中发布的消息。这包括其他用户发布的消息,只要他们是他或她的组队友。

2.我做了什么。

(show.html.erb成员)

<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}

(member.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

(group.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.Errors

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' %>)

1 个答案:

答案 0 :(得分:0)

这是经典!

您在each

使用了map
@timelines = @member.groups.each{|group| group.group_messages}

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

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

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