Avoid multiple render per action

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

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

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

On 12/27/05, Adam D. [email protected] 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.

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.

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

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
:slight_smile: