views:

441

answers:

4

I have the Rails partial which has a few such checks:

 <%= if local_assigns.has_key?(:show_blabla) && show_blabla == false %>
   blabla
 <% end %>

I think that sort of hides the real list of parameters which are used by such partial, and hey local_assigns is not in documentation even.

What are your thoughts?

A: 

My experience in rails is that, whenever you use a feature that is not advertised as the rails way of doing things and thus not widely used it might stop working any time.

On the other hand I sometimes use it when I can't figure out another way to get the divering behaviour.

nasmorn
+1  A: 

I would recommend using defined?(show_blabla) and IMO defined?(bool) && bool == false is perfectly legitimate Ruby.

But if you absolutely have to use local_assigns, it is definitely a hack and most likely a code smell of some sort.

Sam
A: 

Actually, this variable (local_assigns) is documented, albeit hard to find. In fact, according to the rails docs, it's the recommended way to check for defined variables. Check out the rdocs for ActionView::Base

Keith
A: 

Agreeing with Keith, see "Passing local variables to sub templates" in http://caboo.se/doc/classes/ActionView/Base.html

Totor