Rails: Is there an else statement in if try(:current_order)? -
is possible have else statement in if try()?
the else block ignored:
<% if try(:current_order) %> <% if current_order.state != 'complete' && current_order.line_items.count > 0 %> <%= current_order.line_items.count %> <% end %> <% else %> <%= " ".html_safe %> <% end %>
i want achieve print out of
if try(:current_order) fails.
update:
<li class="icon icon-cart"> <%= link_to spree.cart_path %> <div class="cart"> <%= image_tag("shopping_bag.png", class: 'shopping-bag') %> <span class="cart-items-count"> <% if current_order %> <% if current_order.state != 'complete' && current_order.line_items.count > 0 %> <%= current_order.line_items.count %> <% end %> <% else %> <% end %> </span> </div> <% end %> </li>
i use snippet in header , when open static page no current_order available get:
nameerror in spree::pages#show undefined local variable or method `current_order' #<#<class:0x007fcd7636c090>:0x007fcd8770e0c0>
is there easy fix without using try?
why use try
here? use current_order
itself:
<% if current_order %> <% if current_order.state != 'complete' && current_order.line_items.count > 0 %> <%= current_order.line_items.count %> <% end %> <% else %> <% end %>
if current_order
nil
shown.
for record, try
used avoid calling methods on nil
(so e.g. foo.try(:bar)
return nil
if foo
nil
, whereas foo.bar
raise nomethoderror
). not relevant use case here, don't need try
.
Comments
Post a Comment