# Render partials in helper

**URL:** <https://www.ruby-forum.com/t/render-partials-in-helper/145510>\
**Category:** Rails\
**Created:** [August 11, 2008, 3:47am UTC](https://www.ruby-forum.com/t/render-partials-in-helper/145510 "2008-08-11T03:47:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [August 11, 2008, 3:47am UTC](https://www.ruby-forum.com/t/render-partials-in-helper/145510/1 "2008-08-11T03:47:32Z")

</div>

hee people,

Is it possible to render partials in helpers ?

Why I want this:  
I have a menu that list many links but some of those items are children  
of other items.  
so i want to have different partials .

Of course I could set the ruby code in my view but that is not cool (I  
think :P)

I hope someone nows if this is possible

jeljer te wies

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [August 11, 2008, 10:10am UTC](https://www.ruby-forum.com/t/render-partials-in-helper/145510/2 "2008-08-11T10:10:59Z")

</div>

On Aug 11, 2:47 am, jeljer te Wies [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> hee people,
> 
> Is it possible to render partials in helpers ?

Yup, that’s fine. Is there a reason you thought it wouldn’t be  
possible?

Fred

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [August 11, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/render-partials-in-helper/145510/3 "2008-08-11T14:21:12Z")

</div>

On 11 Aug 2008, at 12:42, jeljer te Wies wrote:

> 

That’s syntactically incorrect - it should be  
render :partial =\> ‘menu/test’

If that’s not the problem then you’re going to have to be a little  
more explicit than ‘it didn’t work’

Fred

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [August 11, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/render-partials-in-helper/145510/4 "2008-08-11T13:42:39Z")

</div>

well I tryed to have a helper method like this:

def test  
render :partial ‘menu/test’  
end

this didn’t work

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [August 11, 2008, 2:30pm UTC](https://www.ruby-forum.com/t/render-partials-in-helper/145510/5 "2008-08-11T14:30:58Z")

</div>

Frederick C. wrote:

> On 11 Aug 2008, at 12:42, jeljer te Wies wrote:
> 
> > 
> 
> That’s syntactically incorrect - it should be  
> render :partial =\> ‘menu/test’
> 
> If that’s not the problem then you’re going to have to be a little  
> more explicit than ‘it didn’t work’
> 
> Fred

OOh I am sorry … I wasn’t clear.

what i have now is this:

def set\_menu  
render :partial =\> ‘menu/menu\_item’  
render :partial =\> ‘menu/menu\_item’  
end

I want to render more partials in one helper method. But it renders only  
one partial !..  
so my real question is .

is it possible to render multiple partials in one helper method

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [August 11, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/render-partials-in-helper/145510/6 "2008-08-11T15:27:30Z")

</div>

On Aug 11, 1:30 pm, jeljer te Wies [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Frederick C. wrote:
> 
> > On 11 Aug 2008, at 12:42, jeljer te Wies wrote:
> 
> what i have now is this:
> 
> def set\_menu  
> render :partial =\> ‘menu/menu\_item’  
> render :partial =\> ‘menu/menu\_item’  
> end

The problem here is that you might not quite understandr what happens  
when you do \<%= some\_helper%\>  
Erb basically calls that method, calls to\_s on the result and sticks  
it into the document, so the key thing here is getting your set\_menu  
helper to return the right thing  
When you call render :partial =\> ‘…’ a string is returned containing  
the appropriate data. In ruby the value returned by a function is  
whatever the last statement evaluated, ie the second call to render.  
If you want to return the concatenation of those two things then you  
need to stick them together yourself, eg

def set\_menu  
render( :partial =\> ‘menu/menu\_item’) +  
render( :partial =\> ‘menu/menu\_item’)  
end

Fred

PS: if that was your original question, saying so from the start would  
have saved time for everyone involved.

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [August 11, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/render-partials-in-helper/145510/7 "2008-08-11T16:07:24Z")

</div>

yes you are right…  
But at first I thought that the problem was that it wasn’t possible to  
render partials at al.  
(and I couldn’t find the answer on google)

thanxs ! allot …  
and I hope to answer questions on this forum as soon as I have a better  
understanding of ruby and rails.
