8

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?

5
  • Which versions of ruby and rails are you using, I don't get this behaviour when running 3.1? Sep 3, 2011 at 0:40
  • 3.1. I was using an RC candidate but I upgraded just to be sure and it still isn't working.
    – Msencenb
    Sep 3, 2011 at 1:42
  • Hmmm... So turns out I was defining this helper method in the application_controller using helper_method :run_test however moving it to application_helper file in the helpers folder works. So.. I have a whole in my understanding between the difference of a helper defined in the application controller and a helper defined in a helper file. Anyone able to fill me in?
    – Msencenb
    Sep 3, 2011 at 1:50
  • 1
    A method in the application controller is only available to controllers. A method in a helper is only available to views.
    – drummondj
    Sep 3, 2011 at 2:17
  • 1
    Thanks John. If you want to add that as an answer I'll gladly mark it as accepted for you :)
    – Msencenb
    Sep 3, 2011 at 2:40

3 Answers 3

9

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.

5

Looks like in Rails 3.2 this just works:

# application_helper.rb
def render_my_partial
  render "my_partial"
end
3
  • 1
    This also works in Rails 3.0. This looks like the best answer to me.
    – sockmonk
    Sep 12, 2013 at 18:09
  • 1
    I had to use render "my_partial.html.erb" on a partial named "_my_partial.html.erb" but this worked. Sep 18, 2013 at 22:13
  • The question is specifically how to render a partial in a method on the controller exposed by the controller's helper_method. This doesn't address the question. Jan 9, 2015 at 15:24
-2

You could try using render_to_string method in the view helper

render_to_string :partial => test_name, :layout => false
2
  • 4
    render_to_string is not available in view helpers.
    – YWCA Hello
    Apr 5, 2012 at 17:04
  • For me, this worked. render_partial "/static/partials/rightCol/_related" Aug 24, 2021 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.