過去兩天我試圖得到一個祖先寶石的縈繞。我檢查了一兩個教程,但他們都很老,有很多錯誤或做沒有工作。我已經能夠獲得一點血統,但我想要如何正確定義父母,孩子,根和孫子的一些幫助。我已閱讀文檔,但我無法得到它的適當的手我的項目流程是。使用祖先寶石在紅寶石中創建親子孫子關係
---- 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> </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控制檯得到一個想法,但還有另一個問題。例如 如果一個父母包含多個孩子,它會顯示結果,但如果一個孩子包含孩子,它不會顯示任何東西,儘管當一個父孩子顯示所有的孫輩也得到顯示。我有點困惑這個。任何幫助表示讚賞
你能顯示'@ messages'變量的聲明嗎? – lcguida
忘了添加it.here它是 – soldiershin
我無法理解你想要在這裏實現什麼。你得到所有消息並列出它們。在你的代碼中你看到了顯示這個父母/孩子關係的地方? – lcguida