Undefined method "xxx" of a model when calling a helper

Hi all

I’m creating a shop page. I have a cart model that looks like the
following:

class Cart
attr_reader :line_items

def get_line_item id
@line_items.each do |l|
return l if l.id == id
end
end

def initialize
@line_items = []
end

def empty!
initialize
end

def empty?
return true if @line_items.empty?
false
end
end

And I have some helper methods in application_helper.rb:

module ApplicationHelper
def find_cart
session[:cart] ||= Cart.new
end

def empty_cart
find_cart.empty!
end

def cart_empty?
return true if find_cart.empty?
end
end

Then I want to display a partial form within the layout
application.rhtml but only if the cart is not empty:

<%= render(:partial => ‘warenkorb/warenkorb’, :locals => {:cart =>
find_cart}) unless cart_empty? %>

But this displays me the following error:

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?”!

Thanks for help,
Joshua

Joshua M. wrote:

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?

Hi –

On Sun, 3 Sep 2006, Joshua M. wrote:

@line_items.each do |l|
return l if l.id == id
end
end

def get_line_item(id)
  @line_items.find {|l| l.id == id }
end

false
end

def empty?
  @line_items.empty?
end

end

And I have some helper methods in application_helper.rb:

module ApplicationHelper
def find_cart
session[:cart] ||= Cart.new

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 :slight_smile: (see above) Could the problem have
something to do with caching and then changing the model file?

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org