Calling methods from an erb template?


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 :slight_smile: :

  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,

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

module YourControllerNameHelper.rb

def do_menu_bar
#do processing
return “text to render”



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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs