# Calling render\_to\_string from a view

**URL:** <https://www.ruby-forum.com/t/calling-render-to-string-from-a-view/97259>\
**Category:** Rails\
**Created:** [July 6, 2006, 12:35am UTC](https://www.ruby-forum.com/t/calling-render-to-string-from-a-view/97259 "2006-07-06T00:35:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Austin\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@Austin\_McKinley](https://www.ruby-forum.com/u/Austin_McKinley)\
**Post date:** [July 6, 2006, 12:35am UTC](https://www.ruby-forum.com/t/calling-render-to-string-from-a-view/97259/1 "2006-07-06T00:35:00Z")

</div>

I’m playing around with a list of todos, which I want to render as

's that can nest to an arbitrary level, so I wrote this as a helper method:

```
def print_todo(t)
    str = "<ul>\n\t<li>"

    str += render_to_string(:partial => 'todo', :object => t) + "\n"
    t.children.each { |c| str += print_todo(c) }

    str += "\n\t</li>\n</ul>"
    return str
end

```

Unfortunately, I can’t call this from the view because of the  
render\_to\_string call. What’s the correct way to write this kind of  
functionality? I need the list elements to set up indentation, but I’ve  
also got a clean little partial for the actual contents.

Thanks!  
Austin

---

<div class="post-metadata">

**Author:** ![Austin\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@Austin\_McKinley](https://www.ruby-forum.com/u/Austin_McKinley)\
**Post date:** [July 6, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/calling-render-to-string-from-a-view/97259/2 "2006-07-06T12:01:03Z")

</div>

Austin McKinley wrote:

> I’m playing around with a list of todos, which I want to render as
> 
> 's that can nest to an arbitrary level, so I wrote this as a helper method:
> 
> ```
> def print_todo(t)
> str = "<ul>\n\t<li>"
> 
> str += render_to_string(:partial => 'todo', :object => t) + "\n"
> t.children.each { |c| str += print_todo(c) }
> 
> str += "\n\t</li>\n</ul>"
> return str
> end
> 
> ```
> 
> Unfortunately, I can’t call this from the view because of the  
> render\_to\_string call. What’s the correct way to write this kind of  
> functionality? I need the list elements to set up indentation, but I’ve  
> also got a clean little partial for the actual contents.
> 
> Thanks!  
> Austin

Interesting. Would it not be possible to just nest the partials: this  
would be \_todo\_list.rhtml:

- \<%= render :partial=\>"todo", :object=\>t \<% t.children.each do |child| -%\> \<%= render: partial=\>"todo\_list", :object=\>child %\> \<% end -%\>

I just tested this recursion in a simple way - it definitely works in  
principle 🙂

Cheers,

Robert J.

---

<div class="post-metadata">

**Author:** ![Austin\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@Austin\_McKinley](https://www.ruby-forum.com/u/Austin_McKinley)\
**Post date:** [July 6, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/calling-render-to-string-from-a-view/97259/3 "2006-07-06T17:08:17Z")

</div>

Robert J. wrote:

> Interesting. Would it not be possible to just nest the partials: this  
> would be \_todo\_list.rhtml:
> 
> - \<%= render :partial=\>"todo", :object=\>t \<% t.children.each do |child| -%\> \<%= render: partial=\>"todo\_list", :object=\>child %\> \<% end -%\>
> 
> I just tested this recursion in a simple way - it definitely works in  
> principle 🙂
> 
> Cheers,
> 
> Robert J.

Yep, that works perfectly! Nice solution 🙂

Thanks,  
Austin

---

<div class="post-metadata">

**Author:** ![Austin\_McKinley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@Austin\_McKinley](https://www.ruby-forum.com/u/Austin_McKinley)\
**Post date:** [July 6, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/calling-render-to-string-from-a-view/97259/4 "2006-07-06T18:03:22Z")

</div>

> > 
> 
> Austin  
> If you take a look at components I think you’ll find an even more  
> straightforward way of doing this that allows you to keep render calls  
> in the controller.

see the link below for the api docs about them, I think they’re neat.  
[http://api.rubyonrails.com/classes/ActionController/Components.html](http://api.rubyonrails.com/classes/ActionController/Components.html)

Cheers,  
Chuck V.
