This solution may be misleading, but the Ruby syntax allows you to do this:
<% @family.children.each.with_index(1) do |family_member, index| %> // HTML HERE <% end unless @family.blank? %>
I use this solution only for simple operators, such as checking for the presence of an object (for example, in your case). I do not recommend this solution for more complex logic , because a third party does not know that the condition is at the end of the block.
Other:
<% (@family.try(:children) || []).each.with_index(1) do |family_member, index| %>
If @family is nil , try(:children) does not result in an error, but returns nil , and then nil || [] nil || [] will return an empty array, which "you can loop on it" (the cycle on it is zero times actually).
source share