This project is archived and is in readonly mode.

#1522 ✓invalid

render_to_string doesn't use :locals option

Reported by barunio | December 7th, 2008 @ 03:35 AM | in 2.x

The Rails API says of render_to_string "Renders according to the same rules as render, but returns the result in a string instead of sending it as the response body to the browser. "

But this isn't so -- it doesn't use the same rules as render because render accepts the :locals variable, but render_to_string ignores it.

Is there a reason why render_to_string doesn't allow locals or is this an oversight/ bug?

Comments and changes to this ticket

  • Frederick Cheung

    Frederick Cheung December 7th, 2008 @ 12:59 PM

    • State changed from “new” to “invalid”

    I just did a quick experiment and this seemed to work ok (on 2.2.2) . Can you construct a failing test/example to show exactly how it isn't working for you?

  • barunio

    barunio December 10th, 2008 @ 09:41 PM

    i thought maybe there was a change in rails 2.2 that would have made this work, but i just updated to 2.2.2 and this is still not working for me. here is some very simple code to demonstrate:

    I have a ReportsController, with an view in the views/reports/ folder called "show.xml.builder"

    In my controller:

    x = render_to_string(:action => 'reports/show.xml.builder', :locals => {:blah => 'hi'})

    In the view:

    puts blah

    I get a "undefined local variable or method blah'" error (because the local variable is never defined for the view).

  • Frederick Cheung

    Frederick Cheung December 10th, 2008 @ 10:01 PM

    :locals only exists for render :partial

  • barunio

    barunio December 17th, 2008 @ 07:46 AM

    actually, locals is accepted when rendering a template as well as a partial - but not for rendering an action, apparently. as reference, from the api docs:

    Renders the template with a local variable

    render :template => "weblog/show", :locals => {:customer =>}

    i did get render_to_string working with local variables when i render a template, which is fine for my needs. not sure what the logic is behind allowing locals for rendering a template but not an action...

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket

Referenced by