# Crazy looping in render partial

**URL:** <https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237>\
**Category:** Rails\
**Created:** [January 18, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237 "2007-01-18T13:13:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237/1 "2007-01-18T13:13:00Z")

</div>

Controller:

Personcontroller.rb

def index  
@picks =Product.find\_person(session[:user\_id])  
#@picks=@pickstemp.uniq  
@picks.sort\_by{|x| x.id}.reverse  
render :partial =\> “pick”, :collection =\> @picks  
end

View:

\_pick.rhtml

\<% for pick in @picks %\>  
[\<IMG SRC=“\<%=  
pick.image\_url1%\>” ALT=“” WIDTH=90 HEIGHT=90 BORDER=10](http://example.com/admin/show/<%=pick.id%>)  
\<% end %\>

I suppose I should get each pick once in the view./personal  
But it generates an html repeating @picks over and over again until the  
page is full.

Am I missing any parameter here so it behaves like this?

Any help are highly appreciated.

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237/2 "2007-01-18T13:27:51Z")

</div>

Ouch… why use an tag? Do this instead:

 

\<%= link\_to image\_tag(pick.image\_url1, :width =\> 90, :height =\> 90,  
:border =\> 10), :controller =\> “admin”, :action =\> “show”, :id =\> pick  
%\>

 

On Jan 18, 1:13 pm, Bontina C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237/3 "2007-01-18T13:27:52Z")

</div>

Oh, by the way, I’d say @picks = Product.find\_person(session[:user\_id],  
:order =\> “id”).reverse is better than doing @pics.sort\_by later on.

Whats the output of @picks.inspect?

On Jan 18, 1:22 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237/4 "2007-01-18T13:33:08Z")

</div>

On Jan 18, 2007, at 7:13 AM, Bontina C. wrote:

> But it generates an html repeating @picks over and over again until  
> the  
> page is full.
> 
> Am I missing any parameter here so it behaves like this?
> 
> Any help are highly appreciated.

Change it to:

\_pick.rhtml

[![]()](http://example.com/admin/show/<%=pick.id%>)

The :collection part of the render implies the loop “for pick in  
@picks” (actuallly, the @picks comes from the :collection and the  
‘pick’ from the :partial).

And you probably should use link\_to(image\_tag(…), …) as  
previously suggested.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
Skype: rob.biedenharn

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237/5 "2007-01-18T13:41:10Z")

</div>

On Jan 18, 2007, at 7:13 AM, Bontina C. wrote:

> Controller:
> 
> Personcontroller.rb
> 
> def index  
> @picks =Product.find\_person(session[:user\_id])  
> #@picks=@pickstemp.uniq  
> @picks.sort\_by{|x| x.id}.reverse  
> render :partial =\> “pick”, :collection =\> @picks  
> end

And to possibly save you your next message,  
@picks.sort\_by{|x| x.id}.reverse  
doesn’t change @picks.

Perhaps you want:

def index  
@picks =Product.find\_person(session[:user\_id]).sort\_by {|p| - p.id }  
render :partial =\> “pick”, :collection =\> @picks  
end

Note the use of ‘- p.id’ to reverse the order in one step. It also  
seems odd (to me at least) that “find\_person” returns more than one  
object since it is singular; “find\_people” might be clearer. Of  
course, that just makes me wonder why the Product model is finding a  
Person at all.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
Skype: rob.biedenharn

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237/6 "2007-01-18T13:46:40Z")

</div>

Thank you for cleaning up the puzzles.

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237/7 "2007-01-18T13:48:51Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Oh, by the way, I’d say @picks = Product.find\_person(session[:user\_id],  
> :order =\> “id”).reverse is better than doing @pics.sort\_by later on.
> 
> Whats the output of @picks.inspect?
> 
> On Jan 18, 1:22 pm, “removed\_email\_address@domain.invalid”

Thanks for all the suggestions.  
My code is (hopefully XD) now a little more beautiful.

Abon

---

<div class="post-metadata">

**Author:** ![abon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/45deac/32.png) [@abon](https://www.ruby-forum.com/u/abon)\
**Post date:** [January 18, 2007, 1:54pm UTC](https://www.ruby-forum.com/t/crazy-looping-in-render-partial/82237/8 "2007-01-18T13:54:53Z")

</div>

HAHA! You’re predicting my posting behavior.  
Thanks for the correction and the suggestion about the naming  
convention.
