Partials retaining reference to previously defined locals

2 views
Skip to first unread message

Chris Roos

unread,
Sep 18, 2006, 9:07:48 AM9/18/06
to Rails Core
Hi,

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
@@template_args.

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
persisted.

Any suggestions on what should be happening?

Cheers,

Chris

Andy Watts

unread,
Jan 29, 2008, 10:18:16 PM1/29/08
to rubyonra...@googlegroups.com
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?

cheers
Andy
--
Posted via http://www.ruby-forum.com/.

blj

unread,
Jan 29, 2008, 11:26:55 PM1/29/08
to Ruby on Rails: Core
If this is happening then it is an issue in an unexpected behaviour
way.

On Jan 30, 8:18 am, Andy Watts <ruby-forum-incom...@andreas-s.net>
wrote:

Stefan Kaes

unread,
Feb 2, 2008, 4:10:20 AM2/2/08
to rubyonra...@googlegroups.com
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:

   local_assigns.include?(:somevar)

-- stefan kaes

Reply all
Reply to author
Forward
0 new messages