Forum: Ruby on Rails avoid multiple render per action

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F15fdc7cb2e911b3808837f2be244add?d=identicon&s=25 Adam Denenberg (Guest)
on 2005-12-27 21:06
(Received via mailing list)
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
F15fdc7cb2e911b3808837f2be244add?d=identicon&s=25 Adam Denenberg (Guest)
on 2005-12-28 03:26
(Received via mailing list)
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
D6d48c40f1aee60c7dbfba68770148b2?d=identicon&s=25 Anocha Yimsiriwattana (Guest)
on 2005-12-28 04:08
(Received via mailing list)
Adam Denenberg 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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2005-12-28 05:11
(Received via mailing list)
On 12/27/05, Adam Denenberg <straightflush@gmail.com> 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.
54079122b67de9677c1f93933ce8b63a?d=identicon&s=25 Mitchell Hashimoto (mitchellh)
on 2005-12-28 07:18
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.
F15fdc7cb2e911b3808837f2be244add?d=identicon&s=25 Adam Denenberg (Guest)
on 2005-12-28 14:34
(Received via mailing list)
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
54079122b67de9677c1f93933ce8b63a?d=identicon&s=25 Mitchell Hashimoto (mitchellh)
on 2005-12-28 17:14
Adam Denenberg 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
:)
This topic is locked and can not be replied to.