Forum: Ruby on Rails NoMethodError in Store#add_to_cart

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.
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 mailing (Guest)
on 2005-11-25 17:35
(Received via mailing list)
Dear all,

I'm Going, by the book, through the book "Agile Web Development 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!
5cfd7e9223637ea26c81c2cc3e43799e?d=identicon&s=25 craig (Guest)
on 2005-11-25 20:37
(Received via mailing list)
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 Webster | t: +44 (0)131 516 8595 | e: craig@xeriom.net
Xeriom.NET    | f: +44 (0)709 287 1902 | w: http://xeriom.net
588b32371b62ebe36d70fa712a1173f0?d=identicon&s=25 gerard (Guest)
on 2005-11-26 11:28
(Received via mailing list)
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: <table>
4: <%
5: for item in @items
6: 	product = item.product
7: -%>
8: 	<tr>
9: 		<td><%= item.quantity %></td>


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 Webster tried to type something
like:
> Post your method (add_to_cart) body please. I'm guessing that it's
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!
5cfd7e9223637ea26c81c2cc3e43799e?d=identicon&s=25 craig (Guest)
on 2005-11-26 12:52
(Received via mailing list)
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 Webster | t: +44 (0)131 516 8595 | e: craig@xeriom.net
Xeriom.NET    | f: +44 (0)709 287 1902 | w: http://xeriom.net
B4a3fe24e9fb82df573ae5b38aa3b872?d=identicon&s=25 dez (Guest)
on 2005-11-26 13:25
(Received via mailing list)
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.
588b32371b62ebe36d70fa712a1173f0?d=identicon&s=25 gerard (Guest)
on 2005-11-26 14:45
(Received via mailing list)
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 Webster tried to type
something
like:
> should concentrate our efforts towards the code that makes the app cry.
>
> Yours,
> Craig
> --
> Craig Webster | t: +44 (0)131 516 8595 | e: craig@xeriom.net
> Xeriom.NET    | f: +44 (0)709 287 1902 | w: http://xeriom.net

--
$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!
588b32371b62ebe36d70fa712a1173f0?d=identicon&s=25 gerard (Guest)
on 2005-11-26 14:49
(Received via mailing list)
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 Chesterfield tried to type
something
like:
>                  end
> and see what's displayed in Webrick's log.
> >     redirect_to(:action => 'display_cart')
> >
> > 8: 	<tr>
> >
> >>> 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!
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 mailing (Guest)
on 2005-11-26 15:21
(Received via mailing list)
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: <table>
4: <%
5: for item in @items
6: 	product = item.product
7: -%>
8: 	<tr>
9: 		<td><%= item.quantity %></td>


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 Webster tried to type something
like:
> Post your method (add_to_cart) body please. I'm guessing that it's
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> 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!
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 mailing (Guest)
on 2005-11-26 15:21
(Received via mailing list)
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 Webster tried to type
something
like:
> should concentrate our efforts towards the code that makes the app cry.
>
> Yours,
> Craig
> --
> Craig Webster | t: +44 (0)131 516 8595 | e: craig@xeriom.net
> Xeriom.NET    | f: +44 (0)709 287 1902 | w: http://xeriom.net

--
$biz = http://www.gp-net.nl ;
$fun = http://www.mrmental.com ;
~
~
:wq!
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 mailing (Guest)
on 2005-11-26 15:21
(Received via mailing list)
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 Chesterfield tried to type
something
like:
>                  end
> and see what's displayed in Webrick's log.
> >     redirect_to(:action => 'display_cart')
> >
> > 8: 	<tr>
> >
> >>> 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!
5cfd7e9223637ea26c81c2cc3e43799e?d=identicon&s=25 craig (Guest)
on 2005-11-26 15:25
(Received via mailing list)
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 Webster | t: +44 (0)131 516 8595 | e: craig@xeriom.net
Xeriom.NET    | f: +44 (0)709 287 1902 | w: http://xeriom.net
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 mailing (Guest)
on 2005-11-26 15:29
(Received via mailing list)
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 Chesterfield tried to type
something
like:
>                  end
> and see what's displayed in Webrick's log.
> >     redirect_to(:action => 'display_cart')
> >
> > 8: 	<tr>
> >
> >>> You have a nil object when you didn't expect it!
> >> --
> > --
> > http://lists.rubyonrails.org/mailman/listinfo/rails
--

~
~
:wq!

_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 mailing (Guest)
on 2005-11-26 15:37
(Received via mailing list)
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 Webster 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!
5cfd7e9223637ea26c81c2cc3e43799e?d=identicon&s=25 craig (Guest)
on 2005-11-26 17:02
(Received via mailing list)
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 Webster | t: +44 (0)131 516 8595 | e: craig@xeriom.net
Xeriom.NET    | f: +44 (0)709 287 1902 | w: http://xeriom.net
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 mailing (Guest)
on 2005-11-28 10:57
(Received via mailing list)
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 Webster 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
> Rails@lists.rubyonrails.org
> 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!
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 mailing (Guest)
on 2005-11-28 11:34
(Received via mailing list)
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 Webster 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
> Rails@lists.rubyonrails.org
> 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!
This topic is locked and can not be replied to.