我想在组页面的页面上显示消息..但是

时间:2013-09-26 03:03:50

标签: ruby-on-rails

1.我想在群组页面上显示来自已加入群组的成员的消息。

2.然后,我编写了如下代码,

(适用show.html.erb组)

    <% @group_messages.each do |gm| %>
      <div class="message_area">
      <p class= "member_name"><a href="/members/<%= gm.member_id %>">
       <%= gm.member_id %></a></p>
      <p class="message_content"><%= gm.content %></p>
      <p class="message_create">(<%= gm.created_at.strftime'%Y-%m-%d %H:%M' %>)
      </p></div>
      </tr>
    <% end %>

(适用groups_controller)

def show
    @group = Group.find(params[:id])

    if !checklogin? then return end
    groupfriends = GroupInMember.where(:member_id => session[:login].id)
    gflist = '('
    groupfriends.each do |gf|
      gflist += gf.member_id.to_s + ","
    end
    gflist += session[:login].id.to_s + ')'
    @query = "select * from group_in_members where member_id in " +  gflist 
    @group_messages = GroupMessage.find_by_sql(@query)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @group_messages }
    end



    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @group }
    end
  end

3.然后,我收到了类似的错误消息

missing attribute: content
Extracted source (around line #23):

20:   <div class="message_area">
21:   <p class= "member_name"><a href="/members/<%= gm.member_id %>">
22:    <%= gm.member_id %></a></p>
23:   <p class="message_content"><%= gm.content %></p>
24:   <p class="message_create">(<%= gm.created_at.strftime'%Y-%m-%d %H:%M' %>)
25:   </p></div>
26:   </tr>

4.我怎么解决这个问题?你能给我一个暗示吗?

2 个答案:

答案 0 :(得分:0)

您正在查找邮件时从“group_in_members”表中选择列。

答案 1 :(得分:0)

missing attribute错误表示您执行的查询未包含您在视图中调用的属性。

您正在select * from group_in_members类上运行的查询中使用GroupMessage。在我看来,你应该使用select * from group_messages代替。

或者,如果您想使用ActiveRecord方式并在此过程中使其更清洁,

gflist = groupfriends.pluck(:member_id)
gflist << session[:login].id
@group_messages = GroupMessage.where(:member_id => gflist)