2015-01-05 41 views
0

過去兩天我試圖得到一個祖先寶石的縈繞。我檢查了一兩個教程,但他們都很老,有很多錯誤或做沒有工作。我已經能夠獲得一點血統,但我想要如何正確定義父母,孩子,根和孫子的一些幫助。我已閱讀文檔,但我無法得到它的適當的手我的項目流程是。使用祖先寶石在紅寶石中創建親子孫子關係

---- Parent1

------ Child1

--------- Grandchild1

------ CHILD2

- --- Parent2

------ Child1

現在我已經得到阿霍德如何創建一個父ID.This爲m y代碼。

def index 
@messages = Message.all 
end 

def show 
    @page = Message.where(:ancestry => params[:id]) 
    end 

    def new 
    @message_id = Message.find(params[:id]) 
    end 

def create 
    @page = Message.new(message_params) 
    @page.parent_id = params[:parent_id] 
    #@page = Message.new(message_params) 

    if @page.save 
     flash[:notice] = "The Mesage was created succeffully" 
     redirect_to(:action => 'index') 
    else 
     render('new') 
    end 
end 

這是顯示數據並可以發佈消息的索引文件。

<div class="Pages index"> 
    <h2>Messages</h2> 

    <%= link_to("Add new Page",{:action => 'new'}, :class => 'action new') %> 

    <table class="listing" summary="Page list"> 
     <tr class="header"> 
      <th>&nbsp;</th> 
      <th>ID</th> 
      <th>Content Title</th> 
      <th>Content</th> 
      <th>Parent Id</th> 
     </tr> 

     <% @messages.each do |page| %> 
     <tr> 
      <td><%= page.id %> 
      <td><%= page.content_title %> 
      <td class="center"><%= page.content %> 
      <td class="center"><%= page.parent_id %> 

      <td class="actions"> 
      <%= link_to("Reply",{:action => 'new',:id => page.id},:class =>'action show') %> 
      <%= link_to("Show",{:action => 'show',:id => page.id},:class =>'action show') %>   

      </td> 
     </tr> 
     <% end %> 
     </table> 
     </div> 

     <div class="Pages new"> 
    <h2> Create Pages</h2> 
    <%= form_for(:message, :url => {:action => 'create'}) do |f| %> 
    <table summary="Page form fields"> 

     <tr> 
      <th>Content_title</th> 
      <td><%= f.text_field(:content_title) %></th> 
     </tr> 
     <tr> 
      <th>Content</th> 
      <td><%= f.text_field(:content) %></th> 
     </tr> 
     </table> 

     <div class="form-buttons"> 
      <%= submit_tag("Submit Form") %> 
      </div> 

      <% end %> 
     </div> 

這是當用戶按下應答鍵通PARENT_ID

<div class="Pages new"> 
    <h2> Create Pages</h2> 
    <%= form_for(:message, :url => {:action => 'create',:parent_id => @message_id}) do |f| %> 
    <table summary="Page form fields"> 
    <tr> 
      <th>ParentID</th> 
      <td><%= f.text_field(:id) %></th> 
     </tr> 
     <tr> 
     <tr> 
      <th>Content Title</th> 
      <td><%= f.text_field(:content_title) %></th> 
     </tr> 
     <tr> 
      <th>Content</th> 
      <td><%= f.text_field(:content) %></th> 
     </tr> 
     </table> 

     <div class="form-buttons"> 
      <%= submit_tag("Submit Form") %> 
      </div> 

      <% end %> 
     </div> 

繼承人的節目文件

<%= link_to("Back to List",{:action => 'index'}, :class => 'action new') %> 

<div class="Pages show"> 
    <h2> Show Pages</h2> 

    <table summary="Pages detail view"> 
      <th>Parent ID</th> 
     </tr> 
     <tr> 
      <th>Message Content Title</th> 
     </tr> 
     <tr> 
      <th>Content</th> 
     </tr> 
     <% @page.each do |page| %> 
     <tr> 
      <td><%= page.id %> 
      <td><%= page.content_title %> 
      <td class="center"><%= page.content %> 
      <td class="center"><%= page.parent_id %> 
     </td> 
     </td> 
     </td> 
     </td> 
     </tr> 
     <%end%> 
     </table> 

現在,這是簡單的回覆文件,但我怎麼選擇根節點和子節點以及如何相應地顯示它們。 我已經試過rails控制檯得到一個想法,但還有另一個問題。例如 如果一個父母包含多個孩子,它會顯示結果,但如果一個孩子包含孩子,它不會顯示任何東西,儘管當一個父孩子顯示所有的孫輩也得到顯示。我有點困惑這個。任何幫助表示讚賞

+0

你能顯示'@ messages'變量的聲明嗎? – lcguida

+0

忘了添加it.here它是 – soldiershin

+0

我無法理解你想要在這裏實現什麼。你得到所有消息並列出它們。在你的代碼中你看到了顯示這個父母/孩子關係的地方? – lcguida

回答

1

對於我所看到的,我不認爲你確實理解樹數據結構。

我建議你閱讀本:http://en.wikipedia.org/wiki/Tree_(data_structure)

爲您例如,您有以下樹:

-parent 1 
-child 1.1 (parent -> parent 1) 
-child 1.2 (parent -> parent 1) 
-granddchild 1.2.1 (parent -> child 1.2) 
-parent 2 
-child 2.1 

所以在這個例子父1和2是根節點(他們沒有父)。孩子1.1和1.2(是父母1的孩子)。所以,如果你想訪問他們,你應該做的:

parent1.children 

grandchild1.2.1是節點child1.2的孩子,它的節點parent1的孫子。

要獲得所有的,從parent1,你應該問的後代,

parent1.descendants 

兄弟姐妹都具有相同的父節點,例如兒童1.2和1.1的孩子。

使用示例:您有child1.1,並且您希望它是兄弟姐妹(兄弟)。而不是做

child1.1.parent.children 

,你會怎麼做:

child1.1.siblings

這些只是一些例子,但爲了充分理解的祖先寶石可用的方法,你真的應該學習和理解樹數據結構

+0

這是我很好,我知道樹如何工作和一切,我不知道如何看到使用血統arils樹例如,如果我調用messages = Message.where(:ancestry => 20),然後call page = messages.children它應該返回所有的孩子,但它說未定義的方法。它不是像我不知道如何使用樹結構,我不知道如何使用祖先設置它 – soldiershin

+0

'Message.where'將返回一組消息記錄。這個集合沒有'children'方法。所以我認爲你很困惑,而不是祖先的寶石。 – lcguida

+0

我真的很感激,如果你給我看一個簡單的例子,使用祖先寶石,所以我想到了.. – soldiershin