# Calling methods from an erb template?

**URL:** <https://www.ruby-forum.com/t/calling-methods-from-an-erb-template/144576>\
**Category:** Rails\
**Created:** [July 31, 2008, 5:15am UTC](https://www.ruby-forum.com/t/calling-methods-from-an-erb-template/144576 "2008-07-31T05:15:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axel\_Bock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@Axel\_Bock](https://www.ruby-forum.com/u/Axel_Bock)\
**Post date:** [July 31, 2008, 5:15am UTC](https://www.ruby-forum.com/t/calling-methods-from-an-erb-template/144576/1 "2008-07-31T05:15:49Z")

</div>

Hi,

I am quite new to rails and ruby, and I have a little issue here: I  
want to write a helper method which generates some erb code, which I  
want to call and then eval from a template.

This is best illustrated with an example. I want a menu bar on a page  
like this:

— in the erb template: —  
\<%  
rails\_code = do\_menu\_bar\_with([:back, :delete, :comment])  
%\>  
\<%= eval(rails\_code) %\>  
— end —

I know this is probably not working, a shitty approach and generally  
flawed, but that’s basically what I need.

So now I have two questions, and I am happy for any answer 🙂 :

1. Where would I put this method “do\_menu\_bar\_…”? it surely does not  
work in ApplicationController.

2. If this method returns erb code, how do I evaluate this after?  
Surely not with eval, I guess …

Thanks in advance & greetings,  
Axel.

---

<div class="post-metadata">

**Author:** ![Axel\_Bock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@Axel\_Bock](https://www.ruby-forum.com/u/Axel_Bock)\
**Post date:** [July 31, 2008, 5:23am UTC](https://www.ruby-forum.com/t/calling-methods-from-an-erb-template/144576/2 "2008-07-31T05:23:25Z")

</div>

I can’t possible tell you how to do that, whatever you’re doing (which  
is, btw, vry unclear)

here’s what you do, open YourControllerNameHelper.rb in the helpers  
folder

module YourControllerNameHelper.rb

def do\_menu\_bar  
#do processing  
return “text to render”

end

end

then you use

\<%= do\_menu\_bar %\>

Don’t ever eval code in the view… just don’t. Why would you want to  
do that anyway?
