Question

I have been followingg the rails cast 262 for the ancestry gem so I can use nested messaging. The issue I have is when I view a message it does not list the other messages from the tree. Message id 26 ancestry is 7 (7 is the message that was responded to). Message id 27 ancestry is 7, 26. When I view the original message id 7, it duplicates the content three separate times. If I view message id 26 it duplicates it's content two times. If I view message id 27 it shows no duplication.

How it is supposed to work is when I view any of these message ids, it should list the content from the ancestry.

I am not sure what I am doing wrong.

show.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>

Inspection log:

{#<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">=>{}}}}
Était-ce utile?

La solution

In _message.html.erb try switching:

<%=h @message.body %>

to:

<%= h message.body %>

The variable that is set up for the partial when MessagesHelper calls render is message.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top