如何显示博客的某个分支?

how display certain branch of blog?

请帮忙解决问题。我使用 gem 'ancestry'。我做了简单的博客。 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

index.html.erb:

<%= nested_messages @messages.arrange(:order => :created_at) %>
<br>
<%= render 'form' %>

架构:

create_table "messages", force: :cascade do |t|
  t.text     "content"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string   "ancestry"
end

我将结果输出到 index.html.erb 包含相关消息的消息树。但我只需要输出某些消息和相关消息。例如对于 id=3

的消息

ps: 对于制作的博客,我使用本教程: http://railscasts.com/episodes/262-trees-with-ancestry

好的,I've written about this before;根据 Railscast 的建议,您必须解决某些问题:

  1. Put it in a partial, not helper (it gives greater autonomy)
  2. You need to make sure your structure is as versatile as you need

--

以下是我将如何实现您想要实现的目标:

#app/controllers/messages_controller.rb
class MessagesController < ApplicationController
   def index
      @messages = Message.all #-> could filter this to only retrieve id=3
      #@messages = Message.find "3" #-> like this
   end
end

#app/views/messages/index.html.erb
<% render @messages.arrange(order: :created_at) %>

#app/views/messages/_message.html.erb
# ... single message output code ... #
<%= render message.children if message.has_children? && message.id == "3" %>

然后您可以将条件功能外推到助手中。如果您更具体地描述您的功能,我将能够帮助您定义它。