1

我一直在关注祖先 gem 的 rails cast 262,所以我可以使用嵌套消息传递。我遇到的问题是,当我查看一条消息时,它没有列出树中的其他消息。消息 id 26ancestry是 7(7 是响应的消息)。消息 id 27ancestry是 7、26。当我查看原始消息 id 7 时,它重复了三个不同的内容。如果我查看消息 id 26,它会重复它的内容两次。如果我查看消息 id 27,它显示没有重复。

它的工作原理是当我查看任何这些消息 ID 时,它应该列出来自ancestry.

我不确定我做错了什么。

显示.html.rb:

<p>
        <strong>From:</strong> 
        <%= @message.sender %>
</p>
<p>
        <strong>To:</strong> 
        <%= @message.recipient %>
</p><div class="message">
  <div class="created_at"><%= @message.created_at.strftime('%B %-d, %Y %l:%M%P')  %></div>
  <div class="content"><P>
<%= nested_messages @message.subtree.arrange(:order => :created_at) %>

  </div>
  <div class="actions">
      <% if @message.recipient == @user %>
      <%= link_to "Reply", new_user_message_path(@user, :parent_id => @message) %><% end %>
     |
    <%= link_to "Delete", [current_user, @message], :confirm => 'Are you sure you want to delete this message?', :method => :delete %>

  </div>
</div>

<%= render "form" %>
<p>
    <% if @message.next %>
      <%= link_to 'Next', user_message_path(current_user, @message.next) %>
    <% end %>
    <% if @message.previous %>
      <%= link_to 'Previous', user_message_path(current_user, @message.previous) %>
    <% end %>
    </p>

<p>
    <% if @message.recipient == @user %>

    <%= link_to "Reply", new_user_message_path(@user, :reply_to => @message.sender.id) %>

     |
    <% end %>
    <%= link_to "Inbox", user_messages_path(current_user, :mailbox=>:inbox)%>
    |
    <%= link_to "Delete", [current_user, @message], :confirm => 'Are you sure you want to delete this message?', :method => :delete %>

</p>

messages_helper.rb:

module MessagesHelper
  def nested_messages(messages)
    messages.map do |message, sub_messages|
      render(message) + content_tag(:div, nested_messages(sub_messages), :class => "nested_messages")
    end.join.html_safe
  end
end

_message.html.erb:

<div class="message">
  <div class="created_at"><%= message.created_at.strftime("%B %d, %Y") %></div>
  <div class="content">
    <%=h @message.body %>
  </div>
</div>

检查日志:

{#<Message id: 7, sender_id: 3, recipient_id: 1, sender_deleted: 0, recipient_deleted: 0, subject: "mountain", body: "okok", read_at: "2013-12-06 21:54:33", container: "draft", created_at: "2013-12-05 19:39:11", updated_at: "2013-12-06 21:54:33", conversation_id: nil, original_message_id: nil, ancestry: nil>=>{#<Message id: 26, sender_id: 1, recipient_id: 3, sender_deleted: 0, recipient_deleted: 0, subject: "why s it", body: "isjdkjdjkd", read_at: "2013-12-20 16:46:26", container: "draft", created_at: "2013-12-20 16:21:00", updated_at: "2013-12-20 16:46:26", conversation_id: nil, original_message_id: nil, ancestry: "7">=>{#<Message id: 27, sender_id: 3, recipient_id: 1, sender_deleted: 0, recipient_deleted: 0, subject: "not sure", body: "i'm just continuing the conversation dude!", read_at: "2013-12-20 16:48:02", container: "draft", created_at: "2013-12-20 16:46:45", updated_at: "2013-12-20 16:48:02", conversation_id: nil, original_message_id: nil, ancestry: "7/26">=>{}}}}
4

1 回答 1

1

_message.html.erb尝试切换:

<%=h @message.body %>

至:

<%= h message.body %>

MessagesHelper调用时为部分设置的变量rendermessage.

于 2013-12-21T22:54:53.603 回答