필자는 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)"오류가 발생합니다.