# Avoid multiple render per action

**URL:** <https://www.ruby-forum.com/t/avoid-multiple-render-per-action/51189>\
**Category:** Rails\
**Created:** [December 27, 2005, 9:06pm UTC](https://www.ruby-forum.com/t/avoid-multiple-render-per-action/51189 "2005-12-27T21:06:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [December 27, 2005, 9:06pm UTC](https://www.ruby-forum.com/t/avoid-multiple-render-per-action/51189/1 "2005-12-27T21:06:43Z")

</div>

Hello,

I have a hash that contains categories, and each category ID is a  
hash of subcategories. What i want to do is dynamically load that  
hash, loop thru it, for each category , subcat run a query against the  
DB, and render a partial. The issue is i put the render:partial in a  
for loop but you can only render once per action. What is the best  
way to loop thru all my categories without a multiple render?

I am trying to avoid things like

@cat1 = Post.find(:conditions =\> “cat = 1”) … especially since my  
categories are dynamic.

and would like to do something like

categoryhash.each { |c|

```
 @entries = Post.find(:all, :conditions => ["catid = ?",c.])
render partial

```

}

Any ideas ?

thanks  
adam

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [December 28, 2005, 3:26am UTC](https://www.ruby-forum.com/t/avoid-multiple-render-per-action/51189/2 "2005-12-28T03:26:18Z")

</div>

sorry to reply to my own post but just trying to see if there is a way  
to do this without putting the logic in the view ?

thanks  
adam

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [December 28, 2005, 4:08am UTC](https://www.ruby-forum.com/t/avoid-multiple-render-per-action/51189/3 "2005-12-28T04:08:25Z")

</div>

Adam D. wrote:

> Any ideas ?
> 
> thanks  
> adam  
> How about create a temporally buffer (Hash or Array?), which keep every  
> thing you need to display, and send it to the view?

Tom

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [December 28, 2005, 5:11am UTC](https://www.ruby-forum.com/t/avoid-multiple-render-per-action/51189/4 "2005-12-28T05:11:28Z")

</div>

On 12/27/05, Adam D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @cat1 = Post.find(:conditions =\> “cat = 1”) … especially since my  
> categories are dynamic.
> 
> and would like to do something like
> 
> categoryhash.each { |c|
> 
> ```
> @entries = Post.find(:all, :conditions => ["catid = ?",c.])
> render partial
> 
> ```
> 
> }

I’m not sure how closely the above actually matches what you’re doing,  
but if it’s close, one option is:

@entries = []  
categoryhash.each do |c|  
@entries \<\< Post.find(:all, :conditions =\> [“catid = ?”, c])  
end  
render(:partial =\> ‘blah’, :collection =\> @entries)

The partial will receive each entry in turn as a local variable named  
‘blah’, and the index (if you need it) as ‘blah\_counter’. If you  
want to use helpers in the partial, you’ll need to assign them to an  
instance variable inside the partial template. e.g.:  
\<% @blah = blah -%\>

The partial will need to be generic enough to deal with multiple  
categories of Post.

If that’s not possible, because you need highly specific behavior for  
each category, you can nest partials, and have the ‘blah’ partial  
delegate to specific ones based on the category.  
I’ve personally never had to invoke that particular option, and I  
think you can avoid it too, with a little work.

It’s too bad there’s not a cool find() helper for the ‘in’, keyword,  
because something like:  
Post.find(:all, :conditions =\> [“catid in (?)”, array\_of\_categories])  
would be useful.

–Wilson.

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [December 28, 2005, 7:18am UTC](https://www.ruby-forum.com/t/avoid-multiple-render-per-action/51189/5 "2005-12-28T07:18:56Z")

</div>

What I do to solve rendering issues is use render\_to\_string and store  
allt he output in a variable, then at the end I do render :text =\>  
@myvariable so it doesnt reparse it or anything.

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [December 28, 2005, 2:34pm UTC](https://www.ruby-forum.com/t/avoid-multiple-render-per-action/51189/6 "2005-12-28T14:34:18Z")

</div>

thanks guys i guess i can try reconstucting a result instance variable  
that  
holds all the query results.

As for the render\_as\_string option, i think that render\_as\_string falls  
under the same guidelines as any render, so multiple render\_as\_strings  
i  
dont _think_ will work… i havent tried though just what the api doc  
says.

thx  
adam

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [December 28, 2005, 5:14pm UTC](https://www.ruby-forum.com/t/avoid-multiple-render-per-action/51189/7 "2005-12-28T17:14:13Z")

</div>

Adam D. wrote:

> thanks guys i guess i can try reconstucting a result instance variable  
> that  
> holds all the query results.
> 
> As for the render\_as\_string option, i think that render\_as\_string falls  
> under the same guidelines as any render, so multiple render\_as\_strings  
> i  
> dont _think_ will work… i havent tried though just what the api doc  
> says.
> 
> thx  
> adam

Multiple render\_to\_strings work fine because no output is actually sent  
🙂
