19

Is it possible to render a partial using a helper method where you can also pass local variables from the view in which the helper method is called? For example, when I include this code directly in the view, it renders the partial properly:

 <%= render :partial => "add_round", :locals => { :f => f } %>

Then I moved it to a helper method:

def addRound
  render :partial => "add_round", :locals => { :f => f }
end

Then I called it from the view again with:

 <%= addRound %>

This did not work with the :locals => { :f => f } included in the code. It returned this error: undefined local variable or method `f'. However, the addRound method did render something with the following:

def addRound
  render :partial => "add_round"
end

Writing it this way allowed me to render partials that didn't require the local variables to be passed through (such as plain text strings). But how can I get it to work with the :locals => { :f => f } included? Is there another way to write that?

Thanks so much.

Josh
  • 530
  • 1
  • 3
  • 12

1 Answers1

31

You need to pass f to addRound

def addRound(f)
  render partial: "add_round", locals: { f: f }
end

and in the view

<%= addRound(f) %>
deefour
  • 34,974
  • 7
  • 97
  • 90
  • Thanks Deefour. That worked perfectly (hopefully was just a rookie mistake on my part). I have a related question here: http://stackoverflow.com/questions/12358993/add-section-to-form-by-rendering-a-partial-when-link-is-clicked. Specifically, I can't get the javascript call on the link_to to do what it is supposed to do (to call the addRound method when clicked). Any ideas on that? Thanks again. – Josh Sep 11 '12 at 02:10
  • Glad to help; looks like you got someone else helping with the other question. Good luck. – deefour Sep 11 '12 at 03:12