You have a nil object when you didn't expect it!

I get the following:

NoMethodError in Shoppingcart#my_shopping_cart

Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #33
raised:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.shopping_cart_items

The line in question is

<% for shopping_cart_item in @shopping_cart.shopping_cart_items %>

Any suggestions?

The line in question is

<% for shopping_cart_item in @shopping_cart.shopping_cart_items %>

Any suggestions?

@shopping_cart is nil. You’ll need to look at the my_shopping_cart
action of the Shoppingcart controller to figure out why that’s not
being set correctly.

Thanks Philip. I’m new here, can you advise on what I need to do prior
to get @shopping_cart set? I’m not sure of the exact steps involved in
accessing the data in ROR.

So I made a change and now I have:

NameError in Shoppingcart#my_shopping_cart

Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #33
raised:

undefined local variable or method `shopping_cart_item’ for
Shopping_cart:Class

Not sure where to go from here.

Here’s what I now have, not sure where to go from here:

NameError in Shoppingcart#my_shopping_cart

Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #24
raised:

undefined local variable or method `shopping_cart_item’ for
Shopping_cart:Class
Extracted source (around line #24):

21:

Traded items:

22:
23: <% if @my_cart
24: for shopping_cart_item in @my_cart.shopping_cart_items %>
25:
26: <t

On Nov 27, 4:24 pm, Joe S. [email protected]
wrote:

21: Traded items:

22:
23: <% if @my_cart
24: for shopping_cart_item in @my_cart.shopping_cart_items %>
25:
26: <t

What does your Shopping_cart class look like?
How is shopping_cart_items defined?


Daniel B.

class Shopping_cart < ActiveRecord::Base
belongs_to :owner,
:class_name => “user”,
:foreign_key => “owner_id”

has_many :shopping_cart_items,
:class_name => “shopping_cart_item”,
:finder_sql => “select ti.* from shopping_cart_items ti,
shopping_carts t
where ti.owner_id = t.owner_id”
end

On Nov 27, 4:47 pm, Joe S. [email protected]
wrote:

end

Your shopping_cart_item (singular) is probably a rails model and it
needs to start with a capital letter as it should be a ruby
constant.

When you define a model or any class in the normal way, you do
something like this
class ShoppingCartItem < ActiveRecord::Base

ShoppingCartItem is a ruby constant because it starts with capital (as
opposed to local variable or method name).
Also, you should probably use camel case instead of underscores for
constant names - that seems to be a convention but with rails it might
be a requirement (not completely sure as I’ve never done it that way).
You’d then refer to this class using class_name =>
‘ShoppingCartItem’ .
Your underlying table can stay as shopping_cart_items, of course.

You could probably dispense with class_name altogether if
“has_many :shopping_cart_items” refers to model ShoppingCartItem.
Rails will infer that for you.


Daniel B.

I got it working, thanks for your help

Daniel,

thank you so much, now I have the message

NameError in Shoppingcart#my_shopping_cart

Showing app/views/shoppingcart/my_shopping_cart.rhtml where line #25
raised:

uninitialized constant Shopping_cart::ShoppingCartItems
Extracted source (around line #25):

23: <% if @my_cart
24:
25: for shopping_cart_item in @my_cart_items.shopping_cart_items %>
26:

My shoppingcart.rb is

class Shopping_cart < ActiveRecord::Base
belongs_to :owner,
:class_name => “user”,
:foreign_key => “owner_id”

has_many :shopping_cart_items,
:class_name => “ShoppingCartItems”,
:finder_sql => “select ti.* from shopping_cart_items ti,
shopping_carts t where ti.owner_id = t.owner_id”
end

and my shoppingcartitem.rb defines

class ShoppingCartItem

I feel like I’m close, but I can’t figure this out. Can you figure out
the missing link?

On Nov 27, 7:11 pm, Joe S. [email protected]
wrote:

I got it working, thanks for your help

Posted viahttp://www.ruby-forum.com/.

You can name your files like: shopping_cart_item.rb but define the
class inside as ‘ShoppingCartItem’. Similarly for shopping_cart.rb
and class ShoppingCart.
Glad it helped.


Daniel B.