0

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

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

(show.html.erb-group)

    <% @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.我该如何解决这个问题?你能给我一个提示吗?

4

2 回答 2

0

missing attribute错误意味着您执行的查询不包含视图中正在调用的属性。

您正在使用select * from group_in_membersGroupMessage类上运行的查询。在我看来,您应该select * from group_messages改用它。

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

gflist = groupfriends.pluck(:member_id)
gflist << session[:login].id
@group_messages = GroupMessage.where(:member_id => gflist)
于 2013-09-26T03:11:36.747 回答
0

查找消息时,您正在从“group_in_members”表中选择列。

于 2013-09-26T03:10:29.600 回答