2

This method:

  def admin_buttons
    render :partial => 'portable/admin/admin_buttons'
  end

returns this (not html_safe) when defined in ApplicationControler and made a helper with helper_method:

["my partial's output "]

But it returns the expected string normally when defined in ApplicationHelper.

I don't understand. This is new behavior as far as I know in rails 3.1

1
  • 1
    Arbitrary controller methods shouldn't be returning rendered views. Commented Sep 25, 2011 at 18:28

1 Answer 1

3

Simply put, don't call the controller's render in helpers. It just does not work that way

render in the controller and render in a helper can't be used interchangeably. This isn't new in Rails 3.1.

When you call render in the controller it eventually does call render on the view, the result of which is stored as its response_body. The response body is eventually returned in the way Rack expects, as a string array (what you see as your output).

These links may shed some more light on how this works:
- The controller's definition of render (metal)
- It's superclass method, where response_body is set (abstract_controller)

Sign up to request clarification or add additional context in comments.

4 Comments

I've never experienced this issue for 3 years until 3.1. Calling render in helpers is one of the primary uses of helpers, since it cleans and dries up a lot of :partial calls throughout the app. I've solved this problem by creating a module and including into ApplicationHelper, since calling render in a controller helper doesn't make any sense anyway.
Yes you're right. Rendering partials is something you do very often in helpers. You just have to make sure you're calling the view's render method, which does the actual work of rendering the template, versus the controller's render method, which renders a response to a dispatched request.
I figured this would be the case when defined as a helper with helper_method, but apparently not. Tnx for your input.
Yea, helper_method is still calling the method on the controller. You could use helper MyHelperModule on the controller, in which case the methods of the module would be mixed into the view context instead.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.