undefined method `empty?’ for #<Cart:0x230020c @line_items=[]>
Where does this error come from? I just don’t get it, the Cart object is
instantiated when calling find_cart, and the Cart class definitely does
have a method called “empty?”!
class Cart
…
def empty?
return true if @line_items.empty?
false
end
end
And I have some helper methods in application_helper.rb:
module ApplicationHelper
def cart_empty?
return true if find_cart.empty?
end
end
Not sure why you’re getting that error. Are you sure you dont have a
nother file redefing the cart class somewhere? It’s coming from
“find_cart.empty?” in your helper method.
Also, I’m not sure if this will help, but those methods could a whole
lot simpler. in your empty? method, @line_items.empty? already returns
true or false so it can be greatly simplified to:
def empty? @line_items.empty?
end
And the same with your cart_empty? helper. But why use a helper for
that when you can ask the cart directly if its empty? It’s less code
and easier to troubleshoot.
<%= render :partial … unless find_cart.empty? %>
Lastly, do all the other method on your cart object work ok?
It seems a little odd to me to be manipulating the session data in a
view (or view helper). You might want to find a way to juggle things
so that this is back in the controller.
end
def empty_cart
find_cart.empty!
end
def cart_empty?
return true if find_cart.empty?
end
def cart_empty?
find_cart.empty?
end
undefined method `empty?’ for #<Cart:0x230020c @line_items=[]>
Where does this error come from? I just don’t get it, the Cart object is
instantiated when calling find_cart, and the Cart class definitely does
have a method called “empty?”!
I’m afraid I can’t duplicate your error. I’ve tried to make up for it
with a few code hints (see above) Could the problem have
something to do with caching and then changing the model file?