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
=> nil
pt 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 <blah-blah>".
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+"<br>" 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)%> <h1><%=bar.to_s%></h1> <% end %>
(disclaimer: of course I know that tail recursion is trivially reduced to loop. And treeview should probably be AJAX-ed)