Re: render mutates locals hash?

Frederick Cheung
2009-06-10T00:49:46Z

On Jun 9, 10:35 pm, Jonathan Rochkind <rails-mailing-l...@andreas-
s.net> wrote:
> If you pass a locals hash into render() ... why would Rails mutate that
> hash, adding things to it? This seems awfully odd to me.
>
Rails has a hash of those local variables that should be created
inside a partial (for example if you are rendering a collection then
the current object in that collection is assigned to the appropriate
local variable name). If you supply a hash it uses that (perhaps it
should dup the hash you provide). This shouldn''t have anything todo
with rendering partials from partials

Fred
> Could have something to do with calling partials inside partials, which
> I am doing. Does the locals hash ''inherit'' to the
''inner'' partial?
> That''s kind of odd.
>
> I just traced a weird bug in my app down to this -- I was storing locals
> in an actual hash data structure, instead of using an anonymous hash,
> and wasn''t counting on rails changing in between calls!
>
> By "freeze"ing the thing that I didn''t expect to get
modified, I could
> get this stack trace pointing to what''s doing the modifying, but I
> haven''t looked at it yet.
>
>
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_view/partial_templa
te.rb:48:in
> `[]=''
>
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_view/partial_templa
te.rb:48:in
> `add_object_to_local_assigns!''
>
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_view/partial_templa
te.rb:9:in
> `initialize''
> --
> Posted viahttp://www.ruby-forum.com/.