# Loops in a helper

**URL:** <https://www.ruby-forum.com/t/loops-in-a-helper/88300>\
**Category:** Rails\
**Created:** [March 9, 2007, 2:30pm UTC](https://www.ruby-forum.com/t/loops-in-a-helper/88300 "2007-03-09T14:30:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [March 9, 2007, 2:30pm UTC](https://www.ruby-forum.com/t/loops-in-a-helper/88300/1 "2007-03-09T14:30:45Z")

</div>

Hello

Why this doesnt work?

application\_helper

def check\_collection(collection,partial\_array,msg)  
if collection.empty?  
msg  
else  
partial\_array.each do |partial|  
render :partial =\> partial  
end  
end  
end

view

\<%=check\_collection(@jobs,[“job\_list”, “search\_job”], “No job offers”)%\>

I know that helper should return only last value, so it wont return 2  
partials but it doesnt even display the last partial, only elements in  
an array.

---

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [March 9, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/loops-in-a-helper/88300/2 "2007-03-09T14:53:08Z")

</div>

Have you tried render :partial =\> partial\_name, :collection =\>  
partial\_array? I dunno that it’ll work in a helper but it’s the way to  
do  
what you’re doing outside of one. Might work inside as well. 🙂

RSL

---

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [March 9, 2007, 3:49pm UTC](https://www.ruby-forum.com/t/loops-in-a-helper/88300/3 "2007-03-09T15:49:54Z")

</div>

Russell N. wrote:

> Have you tried render :partial =\> partial\_name, :collection =\>  
> partial\_array? I dunno that it’ll work in a helper but it’s the way to  
> do  
> what you’re doing outside of one. Might work inside as well. 🙂
> 
> RSL

Thanks for na anwser but this doesnt seem to work also

---

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [March 9, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/loops-in-a-helper/88300/4 "2007-03-09T16:24:26Z")

</div>

Paolo N. wrote:

> If you check what each method returns
> 
> > > [1, 2].each {|n| n}  
> > > =\> [1, 2]
> 
> You can understand why your code is not working.
> 
> partial\_array.inject(’’) { |out, partial| out \<\< (render :partial =\>  
> partial)}
> 
> may work but I’m not sure if is a good solution.
> 
> Paolo

This works fine! Thx

---

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [March 9, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/loops-in-a-helper/88300/5 "2007-03-09T15:58:30Z")

</div>

If you check what each method returns

> > [1, 2].each {|n| n}  
> > =\> [1, 2]

You can understand why your code is not working.

partial\_array.inject(’’) { |out, partial| out \<\< (render :partial =\>  
partial)}

may work but I’m not sure if is a good solution.

Paolo

---

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [March 9, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/loops-in-a-helper/88300/6 "2007-03-09T16:42:41Z")

</div>

> def check\_collection(collection,partial\_array,msg)  
> if collection.empty?  
> msg  
> else  
> partial\_array.each do |partial|  
> render :partial =\> partial  
> end  
> end  
> end  
> you can create a string (str\_output for example) and then do something  
> like  
> str\_output+= render\_to\_string (:partial=\>partial)

finally put at the end of the helper “str\_output”, so that will be your  
return value.

regards,

javier ramirez

- 

* * *

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!
