I have the following code in my application.erb file:
<%= hidden_div_if(@cart.line_items.empty?, :id => "cart") do %>
This works fine unless I load a page that does not receive the @cart.line_items object, when I receive the following error:
undefined method `line_items' for nil:NilClass
How do I rewrite the line from my .erb file to have it behave the same if @cart.line_items is empty and if @cart is nil?
Based on the below answers, I changed my code to use:
<%= hidden_div_if(!@cart.nil? && @cart.line_items.empty?, :id => "cart") do %>
*Updated to match the comment Baldrick added to this question (as it is more concise than my original edit).
Use
@cart.blank?to check whether it is nil or empty. Theblank?method is a rails extension.