我一直在遵循祖先宝石的rails-cast262,这样我就可以使用嵌套消息了。我遇到的问题是,当我查看一条消息时,它不会列出树中的其他消息。消息id 26ancestry
是7(7是响应的消息)。消息id 27ancestry
是7/26。当我查看原始消息id 7时,它会分别复制三次内容。如果我查看消息id 26,它会复制它的内容两次。如果我查看消息id 27,它显示没有重复。
它的工作原理是,当我查看这些消息id中的任何一个时,它应该列出ancestry
中的内容。
我不确定我做错了什么。
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>
检查日志:
{#<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">=>{}}}}
在_message.html.erb
中尝试切换:
<%=h @message.body %>
至:
<%= h message.body %>
当MessagesHelper
调用render
时为分部设置的变量是message
。