Forum: Ruby on Rails recursion in .rhtml views ?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A36864cb890189384d36f627f6c05d9c?d=identicon&s=25 Dmitry Yakovkin (Guest)
on 2007-04-03 13:55
(Received via mailing list)
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<<' '}
>> p
>> a.children.each { |al| pt al }
>> end
=> 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 <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 <<"<br>"
  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)
5c19f2d52879a1e10670c7334ba4c7e3?d=identicon&s=25 Lugovoi Nikolai (Guest)
on 2007-04-03 14:01
(Received via mailing list)
use partials?

  <%= %>
  <ul><%= render :partial => 'node', :collection => node.children

2007/4/3, Dmitry Yakovkin <>:
This topic is locked and can not be replied to.