Should local_assigns.has_key be widely used? partially considered a code smell?

I have a Rails piece that has several checks like this:

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

      

I think this kind hides the real list of parameters that are used so partial, and hey local_assigns

doesn't even apply to the documentation.

What are your thoughts?

+1


a source to share


4 answers


See "Passing Local Variables to Subpatterns" in Class: ActionView :: Base for documentation on local_assigns

.



+2


a source


Actually, this variable (local_assigns) is documented, although difficult to find. In fact, according to the rails docs, this is the recommended way to test certain variables. Check rdocs for ActionView :: Base



+1


a source


My experience with rails is that whenever you use a feature that is not advertised as rails, and therefore not widely used, it might stop working at any time.

On the other hand, I sometimes use it when I can't find another way to get the driving behavior.

0


a source


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

But if you absolutely must use local_assigns, this is definitely a hack and most likely some kind of code smell.

0


a source







All Articles