How do I check if a variable is defined in rails?
<% if defined?(:dashboard_pane_counter) && dashboard_pane_counter.remainder(3) == 0 %> # do_something here, this assumes that dashboard_pane_counter is defined, but not nil<% end %>
When using rails and instance variables, nil has a try
method defined, so you can do:
<% if @dashboard_pane_counter.try(:remainder(3)) == 0 %> #do something<% end %>
so if the instance variable is not defined, try(:anything)
will return nil and therefore evaluate to false. And nil == 0
is false
local_assigns can be used for that, since this question is from a few years ago, I verified that it exists in previous versions of rails
<% if local_assigns[:dashboard_pane_counter] && dashboard_pane_counter.remainder(3) == 0%><% end %>
It's in the notes here