Recursion in .rhtml views?

Hi evrybdy,
I have a very simple Albums hierarchy:

class Album < ActiveRecord::Base
has_many :books

It’s trivial to display a treeview in script/console:

def pt(a)
pre=’’;a.ancestors.size.times {pre<<’ '}
a.children.each { |al| pt al }
=> nil

pt Album.find 1
" 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 <<"
a.children.each { |al| print_tree al,out }

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)%>


<% end %>

(disclaimer: of course I know that tail recursion is trivially reduced
to loop. And treeview should probably be AJAX-ed)

use partials?


  • <%= %>
      <%= render :partial => 'node', :collection => node.children %>
  • 2007/4/3, Dmitry Y. [email protected]: