Forum: Ruby on Rails You have a nil object when you didn't expect it!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe S. (Guest)
on 2008-11-27 05:59
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?
Philip H. (Guest)
on 2008-11-27 06:04
(Received via mailing list)
> 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.
Joe S. (Guest)
on 2008-11-27 06:14
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.
Joe S. (Guest)
on 2008-11-27 06:29
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.
Joe S. (Guest)
on 2008-11-27 07:24
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:   <td valign="top" width="50%"></b>Traded items: </b><br>
22:   <table>
23:   <% if @my_cart
24:     for shopping_cart_item in @my_cart.shopping_cart_items %>
25:     <tr>
26:       <t
Daniel B. (Guest)
on 2008-11-27 07:39
(Received via mailing list)
On Nov 27, 4:24 pm, Joe S. <removed_email_address@domain.invalid>
wrote:
>
> 21:   <td valign="top" width="50%"></b>Traded items: </b><br>
> 22:   <table>
> 23:   <% if @my_cart
> 24:     for shopping_cart_item in @my_cart.shopping_cart_items %>
> 25:     <tr>
> 26:       <t
> --

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


--
Daniel B.
Joe S. (Guest)
on 2008-11-27 07:47
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
Daniel B. (Guest)
on 2008-11-27 08:44
(Received via mailing list)
On Nov 27, 4:47 pm, Joe S. <removed_email_address@domain.invalid>
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.
Joe S. (Guest)
on 2008-11-27 09:45
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:     <tr>

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?
Joe S. (Guest)
on 2008-11-27 10:11
I got it working, thanks for your help
Daniel B. (Guest)
on 2008-11-27 10:22
(Received via mailing list)
On Nov 27, 7:11 pm, Joe S. <removed_email_address@domain.invalid>
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.
This topic is locked and can not be replied to.