Hi evrybdy,
I have a very simple Albums hierarchy:
class Album < ActiveRecord::Base
has_many :books
end
It’s trivial to display a treeview in script/console:
def pt(a)
pre=’’;a.ancestors.size.times {pre<<’ '}
p pre+a.name
a.children.each { |al| pt al }
end
=> nilpt Album.find 1
“root”
" child1"
" subchi1"
" chi2"
" subchi2"
Of course, “p” would not work inside a view, so I google for a while
and find out about “concat”. But concat would not work either with
"undefined _erbout ".
I was stuck for a while until finally this came to mind:
<%
def print_tree(a,out)
pre=’’;a.ancestors.size.times {pre<<’ '}
out << pre+a.name+"
"
a.children.each { |al| print_tree al,out }
end
print_tree Album.find(1), _erbout
%>
So my question is: why is _erbout out of scope in my inside method?
And what is The Proper Rails way for recursive output? Buffer into a
temp variable? Or something else? And why is it not possible to have
HTML output inside method definition? (like:
<% def foo (bar)%>
<%=bar.to_s%>
<% end %>(disclaimer: of course I know that tail recursion is trivially reduced
to loop. And treeview should probably be AJAX-ed)