filter_local_assigns_for_partial

15 views
Skip to first unread message

Heather Moore

unread,
Jan 6, 2012, 6:52:21 PM1/6/12
to erector
Hello!

Today I ran into an interesting problem losing a variable when
rendering a partial.

As it turns out, there is a method that specifically strips local
assigns with the same name as the partial to be rendered. It goes
something like this:

if pink_lady = Apple.find_by_type("pink_lady")
render partial: "apples/pink_lady", locals: { pink_lady:
pink_lady }
end

Later, we get to the method, filter_local_assigns_for_partial, where
pink_lady is rejected from the collection.

def filter_local_assigns_for_partial(widget_class, local_assigns)
widget_class_variable_name = widget_class.name.underscore
widget_class_variable_name = $1 if widget_class_variable_name =~
%r{.*/(.*?)$}

local_assigns.reject do |name, value|
name == :object || name == widget_class_variable_name.to_sym
end
end

Can someone help me understand why this is done?

Thanks,
Heather

Alex Chaffee

unread,
Jan 19, 2012, 4:49:11 PM1/19/12
to hea...@needfeed.com, erector
I'm not sure, but it probably relates to the following, let's be nice
and say "feature," in Rails. Every partial also has a local variable
with the same name as the partial (minus the underscore). You can pass
an object in to this local variable with a parameter named :object.

I can't see Erector's implementation of that in the code snippet you
sent but this is probably related. And I'm not saying the behavior you
described isn't also a bug.

- A

--
Alex Chaffee - al...@stinky.com
http://alexchaffee.com
http://twitter.com/alexch

Alex Chaffee

unread,
Jan 19, 2012, 4:51:09 PM1/19/12
to hea...@needfeed.com, erector
Reply all
Reply to author
Forward
0 new messages