optional local variables in rails partial templates: how do I get out of the (defined? foo) mess?
Posted
by brahn
on Stack Overflow
See other posts from Stack Overflow
or by brahn
Published on 2010-01-13T21:55:16Z
Indexed on
2010/03/30
2:03 UTC
Read the original article
Hit count: 377
ruby-on-rails
|partials
I've been a bad kid and used the following syntax in my partial templates to set default values for local variables if a value wasn't explicitly defined in the :locals hash when rendering the partial --
<% foo = default_value unless (defined? foo) %>
This seemed to work fine until recently, when (for no reason I could discern) non-passed variables started behaving as if they had been defined to nil (rather than undefined).
As has been pointed by various helpful people on SO, http://api.rubyonrails.org/classes/ActionView/Base.html says not to use
defined? foo
and instead to use
local_assigns.has_key? :foo
I'm trying to amend my ways, but that means changing a lot of templates.
Can/should I just charge ahead and make this change in all the templates? Is there any trickiness I need to watch for? How diligently do I need to test each one?
© Stack Overflow or respective owner