Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render partial from helper_method

Ok so I have a helper method in the application controller:

def run_test(test_name)
  #computation stuff
  render :partial => test_name
end

And I call it like so in views:

<%= run_test("testpartial") %>

and it renders ok with only 1 (although... the render partial seems to be returning an array instead of just the partial content?), but if I put the run_test helper call in the view twice I get a double render error, which shouldn't be happening with partials.

Any ideas?

like image 333
Msencenb Avatar asked Sep 02 '11 22:09

Msencenb


2 Answers

render in a controller versus render in a view are different methods. The controller eventually calls render on a view, but the controller's render method itself expects to be called only once. It looks like this:

# Check for double render errors and set the content_type after rendering.
def render(*args) #:nodoc:
  raise ::AbstractController::DoubleRenderError if response_body
  super
  self.content_type ||= Mime[formats.first].to_s
  response_body
end

Note how it raises if called more than once?

When you call helper_method you give the view a proxy to the controller's version of render, which is not intended to be used in the same way as ActionView's, which is, unlike the controller's, expected to be called repeated to render partials and whatnot.

like image 167
numbers1311407 Avatar answered Nov 14 '22 22:11

numbers1311407


Looks like in Rails 3.2 this just works:

# application_helper.rb
def render_my_partial
  render "my_partial"
end
like image 44
fguillen Avatar answered Nov 14 '22 20:11

fguillen