Partials retaining reference to previously defined locals


Problem occurs where partial is rendered with explicit locals.
Subsequent rendering of that partial without those locals, or with
different locals, retains access to the previous keys (NOT values).

I've tracked the problem (if it is a problem) to
ActionView::Base.compile_template_source and the use of

The reason I question whether it is a genuine problem is because maybe
this is known and accepted for performance reasons.

Although, I'm not sure it is particularly problematic, it can cause
unexpected behviour. Example of unexpected behaviour..

* I have two actions, both of which render(:partial => 'my_partial').
The first renders with some locals that my_partial depends upon.
* I visit the first action (that sets locals) and see the partial correctly.
* I visit the second action (that doesn't set locals) and see the
partial correctly. * Unexpected - It should break.

* Restarting the webserver (or running a separate test that doesn't
first call the action with the locals) confirms that the second
action/partial combo is broken.

I'm incredibly unfamiliar with the templating stuff and so have
restrained from submitting some tests, although I do have an example
of showing that access to locals (note, access not the values) are

Any suggestions on what should be happening?



I've noticed this behavior in consecutive calls to the same partial.

1. render :partial => 'blah', :locals => {:somevar => true}
2. render :partial => 'blah'

In the second call to the partial, "defined?(somevar)" will return
true...even tho it wasn't in the locals passed in the render..

I'd like to have optional locals on some of my partials.
The current behavior requires me to always pass the local :somevar.

Does this strike anyone else as an issue?


If this is happening then it is an issue in an unexpected behaviour

You cannot test whether a certain key (:somevar) was present in the locals hash passed to render using defined?(:somevar), due to the way template compilation works in Rails.

But you can use the following pattern instead:


– stefan kaes