NoMethodError in Store#add_to_cart


#1

Dear all,

I’m Going, by the book, through the book “Agile Web D. with
Rails”.
(I hope btw that it’s not off topic to share my mental bump here. )

I’m running into the following error:

NoMethodError in Store#add_to_cart

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

Have checked all the syntaxes several times and I’m stuck on pages
85-86.

Can somebody help a Rails newby out, where to start?

Thanx a million.

Kind reagrds,

Gerard.


“Who cares if it doesn’t do anything? It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;
~
:wq!


#2

On 25 Nov 2005, at 16:32, Gerard wrote:

I’m running into the following error:

NoMethodError in Store#add_to_cart

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

Can somebody help a Rails newby out, where to start?

Post your method (add_to_cart) body please. I’m guessing that it’s
something to do with you instanciating the LineItem object but can’t
tell for sure.

Yours,
Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed_email_address@domain.invalid
Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net


#3

Craig,

First, thanx for the response. Below is the add_to_cart action.

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end

Funny enough it now comes with an different error (a step further since
it’s
redirected there), even though no code was changed in the mean time,
saying:

NoMethodError in Store#display_cart

Showing app/views/store/display_cart.rhtml where line #6 raised:

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

Extracted source (around line #6):

3:


4: <%
5: for item in @items
6: product = item.product
7: -%>
8:
9:

I can’t help to think that an initial value is not set somewhere, since
it
keep talking about a nil object.

Looking forward to your response.

Kind Regards,

Gerard.

On Friday 25 November 2005 20:35, Craig W. tried to type something
like:

Post your method (add_to_cart) body please. I’m guessing that it’s


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!

<%= item.quantity %>

#4

On 26 Nov 2005, at 10:24, Gerard J. Petersen wrote:

Funny enough it now comes with an different error (a step further
since it’s
redirected there), even though no code was changed in the mean
time, saying:

NoMethodError in Store#display_cart

Could you post your display_cart method here? The change you mention
seems to imply that the add_to_cart method is working now so we
should concentrate our efforts towards the code that makes the app cry.

Yours,
Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed_email_address@domain.invalid
Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net


#5

It sounds like @items is not getting set. Check your display_cart
method in your store_controlled.rb. It should be something like:

     def display_cart
             @items=@cart.items
             if @items.empty?
                     redirect_to_index 'Your cart is empty'
             end
             if params[:context]==:checkout
                     render :layout => false
             end
     end

If you are still stuck, put in some debugging lines to see what’s
going on, e.g.

     def display_cart
             @items=@cart.items
	p @cart.inspect
	p @items.inspect

and see what’s displayed in Webrick’s log.


#6

Derek,

This is the display_cart method.

def display_cart
@cart = find_cart
@items = @cart.items
end

It feels like an initial setting is missing. In
app/controllers/application.rb
The following is set.

class ApplicationController < ActionController::Base
model :cart
model :line_item
end

I’ll try your debugging suggestions and get back to you. BTW, what does
the
‘p’ invoke/stand for in the @…inspect lines.

Thanx a lot

Regards,

Gerard.

On Saturday 26 November 2005 13:22, Derek C. tried to type
something
like:

             end

and see what’s displayed in Webrick’s log.

redirect_to(:action => 'display_cart')

8:

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


http://lists.rubyonrails.org/mailman/listinfo/rails

$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!


#7

Craig,

First, thanx for the response. Below is the add_to_cart action.

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end

Funny enough it now comes with an different error (a step further since
it’s
redirected there), even though no code was changed in the mean time,
saying:

NoMethodError in Store#display_cart

Showing app/views/store/display_cart.rhtml where line #6 raised:

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

Extracted source (around line #6):

3:


4: <%
5: for item in @items
6: product = item.product
7: -%>
8:
9:

I can’t help to think that an initial value is not set somewhere, since
it
keep talking about a nil object.

Looking forward to your response.

Kind Regards,

Gerard.

On Friday 25 November 2005 20:35, Craig W. tried to type something
like:

Post your method (add_to_cart) body please. I’m guessing that it’s


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


“Who cares if it doesn’t do anything? It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;
~
:wq!

<%= item.quantity %>

#8

Craig,

Below the display_cart method:

def display_cart
@cart = find_cart
@items = @cart.items
end

No to exiting IMHO.

Thanx

Regards,

Gerard.

On Saturday 26 November 2005 12:49, Craig W. tried to type
something
like:

should concentrate our efforts towards the code that makes the app cry.

Yours,
Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed_email_address@domain.invalid
Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net


$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!


#9

Derek,

This is the display_cart method.

def display_cart
@cart = find_cart
@items = @cart.items
end

It feels like an initial setting is missing. In
app/controllers/application.rb
The following is set.

class ApplicationController < ActionController::Base
model :cart
model :line_item
end

I’ll try your debugging suggestions and get back to you. BTW, what does
the
‘p’ invoke/stand for in the @…inspect lines.

Thanx a lot

Regards,

Gerard.

On Saturday 26 November 2005 13:22, Derek C. tried to type
something
like:

             end

and see what’s displayed in Webrick’s log.

redirect_to(:action => 'display_cart')

8:

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


http://lists.rubyonrails.org/mailman/listinfo/rails

$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!


#10

On 26 Nov 2005, at 13:43, Gerard J. Petersen wrote:

Below the display_cart method:

def display_cart
@cart = find_cart
@items = @cart.items
end

Does your Cart model initialise the items attribute?

Yours,
Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed_email_address@domain.invalid
Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net


#11

Craig,

Below the display_cart method:

def display_cart
@cart = find_cart
@items = @cart.items
end

No to exiting IMHO.

Thanx

Regards,

Gerard.

On Saturday 26 November 2005 12:49, Craig W. tried to type
something
like:

should concentrate our efforts towards the code that makes the app cry.

Yours,
Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed_email_address@domain.invalid
Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net


$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!


#12

Craig,

This, as far as I understand it, is done voor both Admin and Store
controller
in the application.rb. See below:

class ApplicationController < ActionController::Base
model :cart
model :line_item
end

On Saturday 26 November 2005 15:25, Craig W. tried to type
something
like:

Yours,
http://lists.rubyonrails.org/mailman/listinfo/rails

“Who cares if it doesn’t do anything? It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;
~
:wq!


#13

Derek,

This is the display_cart method.

def display_cart
@cart = find_cart
@items = @cart.items
end

It feels like an initial setting is missing. In
app/controllers/application.rb
The following is set.

class ApplicationController < ActionController::Base
model :cart
model :line_item
end

I’ll try your debugging suggestions and get back to you. BTW, what does
the
‘p’ invoke/stand for in the @…inspect lines.

Thanx a lot

Regards,

Gerard.

On Saturday 26 November 2005 13:22, Derek C. tried to type
something
like:

             end

and see what’s displayed in Webrick’s log.

redirect_to(:action => 'display_cart')

8:

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


http://lists.rubyonrails.org/mailman/listinfo/rails

~
~
:wq!


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#14

On 26 Nov 2005, at 14:34, Gerard wrote:

This, as far as I understand it, is done voor both Admin and Store
controller
in the application.rb. See below:

class ApplicationController < ActionController::Base
model :cart
model :line_item
end

This makes sure that the models are available to your application
when it tries to restore the session. You still need to set the
variables to an initial value in Cart as shown in page 84, listing
“File 76.”

After this is done you may need to clear out your ruby sessions.

Yours,
Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed_email_address@domain.invalid
Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net


#15

Craig,

Thanx again for your patience. The variables are being set. When I put
the
following code (page 88) inplace, it seems to work ok this time:

def add_product(product)
item = @items.find {|i| i.product_id == product.id}
if item
item.quantity += 1
else
items = LineItem.for_product(product)
@items << item
end
@total_price += product.price
end

Can the ruby session info mess with this, because earlier this didn’t
work and
now it does. Note: I now also restarted webrick, removed the ruby
session,
and remove the cookie in the browser.

I now end up with the display_cart method that doesn’t work.

NoMethodError in Store#display_cart

Showing app/views/store/display_cart.rhtml where line #6 raised:

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

Two more Q’s:

  • How does it get to the object nil.product?
  • Is there a way to enable extensive logging, because logfiles are very
    usefull.

Thanx very much again for your feedback!

Regards,

Gerard.

On Saturday 26 November 2005 16:58, Craig W. tried to type
something
like:

This makes sure that the models are available to your application
Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


“Who cares if it doesn’t do anything? It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;
~
:wq!


#16

Craig,

Solved it! It was a friggin’ typo.

At “->” the first word shouldn’t be plural. I humbly thank you for
spending
your precious time on my human error … @-)

Kind regards,

Gerard.

def add_product(product)
item = @items.find {|i| i.product_id == product.id}
if item
item.quantity += 1
else
-> items = LineItem.for_product(product)
@items << item
end
@total_price += product.price
end

On Saturday 26 November 2005 16:58, Craig W. tried to type
something
like:

This makes sure that the models are available to your application
Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


“Who cares if it doesn’t do anything? It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;
~
:wq!