2015-02-05 8 views
0

필자는 Ancestry gem을 사용하는 Rails 모델을 기반으로 Windows 탐색기 유형 트리를 HTML 테이블로 그리는 재귀 코드를 생성했습니다. 문자열 연결보다 테이블을 그리는 더 우아한 방법이 있는지 알고 싶습니다. 나는 (반드시 있어야한다고 생각하고) 노력했지만 두 번째 erb 부분에 대한 호출을 얻을 수 없었다. 여기Rails 4를 사용하여 Windows 탐색기 유형의 트리보기를 표시하는 가장 좋은 방법

def index 
    @trees = Tree.all 
    @root = Tree.first.root 
    @table_string = '' 
end 

도우미입니다 :

<table> 
    <thead> 
    <tr> 
     <th>Folder</th> 
     <th>Value</th> 
     <th>Note</th> 
     <th>ID</th> 
     <th>Depth</th> 
     <th>Has Children?</th> 
     <th>Parent</th> 
    </tr> 
    </thead> 
    <tbody> 
    <%= tree_table(@root) %> 
    </tbody> 
</table> 
: 여기
module TreesHelper 

def draw_branch(t) 
    ht = "" 
    ht << '<tr>' 
    ht << "\n\t\t\t" 
    ht << '<td class="data" align="left" style="padding-left: ' 
    ht << ((t.ancestry_depth+1)*15).to_s 
    ht << 'px"><img src="assets/icons/folder.gif" class="icon_folder" width="18">' 
    ht << (link_to t.name, t).to_s 
    ht << '</td>' 
    ht << "\n\t\t\t" 
    ht << '<td class="data" align="right">' 
    ht << number_to_currency(t.value, precision: 0).to_s 
    ht << '</td>' 
    ht << "\n\t\t\t" 
    ht << '<td class="data">' 
    ht << t.note 
    ht << '</td>' 
    ht << "\n\t\t\t" 
    ht << '<td class="data" align="right">' 
    ht << (t.id).to_s 
    ht << '</td>' 
    ht << "\n\t\t\t" 
    ht << '<td class="data" align="right">' 
    ht << (t.ancestry_depth).to_s 
    ht << '</td>' 
    ht << "\n\t\t\t" 
    ht << '<td class="data" align="right">' 
    ht << (t.has_children?).to_s 
    ht << '</td>' 
    ht << "\n\t\t\t" 
    ht << '<td class="data" align="right">' 
    ht << t.parent_id.to_s 
    ht << '</td>' 
    ht << "\n\t\t" 
    ht << '</tr>' 
    ht << "\n\t\t" 
end 

def tree_table(t) 
    # render partial: "branch_as_row", object: t Doesn't work whereas the next line does... 
    @table_string << draw_branch(t) 
    t.children.each do |tt| 
     tree_table(tt) 
    end 
    @table_string.html_safe 
end 
end 

이 도우미 함수를 호출하는 원래의 부분이다 여기에

컨트롤러로 시작, 내 코드입니다

FWIW, 작동하지 않는 부분은 다음과 같습니다 (_branch_as_row.html.erb) :

<tr data-level="<%= t.depth+1%>"> 
    <td class="data" align="left" style="padding-left: <%=((t.ancestry_depth+1)*10).to_s%>px"><img src="assets/icons/folder.gif" width="14"></td> 
    <td class="data" align="right"><%= t.id %></td> 
    <td class="data" align="right"><%= t.ancestry_depth %></td> 
    <td class="data" align="right"><%= t.has_children? %></td> 
    <td class="data"><%= link_to t.name, t %></td> 
    <td class="data" align="right"><%= number_to_currency(t.value, precision: 0) %></td> 
    <td class="data"><%= t.note %></td> 
    <td class="data" align="right"><%= t.parent_id %></td> 
</tr> 

"잘못된 수의 인수 (0의 경우 1..2)"오류가 발생합니다.

답변

0

이것은 좋지 않을 수도 있지만 질문을 작성한 후 공식 문서를 다시 읽으면 다르게 생각하고 스스로 질문에 답변했습니다.

<table id="tree" border="1" cellspacing="0" cellpadding="6" class="table table-hover table-bordered"> 
    <thead> 
     <tr data-level="header" class="header"> 
      <th>Folder</th> 
      <th>ID</th> 
      <th>Depth</th> 
      <th>Has Children?</th> 
      <th>Folder</th> 
      <th>Value</th> 
      <th>Note</th> 
      <th>Parent</th> 
     </tr> 
    </thead> 
    <tbody> 
<%= render partial: "branch_as_row", object: @root, as: :t %> 
    </tbody> 
</table> 

여기에 어떤 (약간 개선) 부분이 자신을 호출입니다 :

여기 내보기는 지금

<tr data-level="<%= t.depth+1%>"> 
    <td class="data" align="left" style="padding-left: <%=((t.ancestry_depth+1)*15).to_s%>px"><img src="assets/icons/folder.gif" width="18" class="icon_folder"><%= link_to t.name, t %></td> 
    <td class="data" align="right"><%= t.id %></td> 
    <td class="data" align="right"><%= t.ancestry_depth %></td> 
    <td class="data" align="right"><%= t.has_children? %></td> 
    <td class="data"><%= link_to t.name, t %></td> 
    <td class="data" align="right"><%= number_to_currency(t.value, precision: 0) %></td> 
    <td class="data"><%= t.note %></td> 
    <td class="data" align="right"><%= t.parent_id %></td> 
</tr> 
<% if t.has_children? %> 
    <%= render partial: "branch_as_row", collection: t.children, as: :t %> 
<% end %> 

추악한 문자열 연결 방법보다 훨씬 더! 획기적인 점은 부분이 스스로를 호출 할 수 있고 부분 구문에 변수를 전달하기 위해 적절한 구문을 사용한다는 점입니다.