small medium large xlarge

Tis_me_pragsmall
25 Jul 2008, 00:06
Steve Fairhurst (17 posts)

I am following the book very closely and I am up to ITERATION C2 : A SMARTER CART (page 113). I have just made the changes to the add_to_cart.html.erb and I now get:

NoMethodError in Store#add_to_cart

Showing store/add_to_cart.html.erb where line #4 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.quantity
Extracted source (around line #4):

1: <h1>Your Pragmatic Cart</h1>
2: <ul>
3:   <% for item in @cart.items %>
4:     <li><%= item.quantity %> &times; <%=h item.title %></li>
5:   <% end %>
6: </ul>
RAILS_ROOT: /Users/Steve/rails/depot

Application Trace | Framework Trace | Full Trace
app/views/store/add_to_cart.html.erb:4:in `_run_erb_47app47views47store47add_to_cart46html46erb'
app/views/store/add_to_cart.html.erb:3:in `each'
app/views/store/add_to_cart.html.erb:3:in `_run_erb_47app47views47store47add_to_cart46html46erb'

So I continued on and tried the “rake db:sessions:clear”, still get the error. I have been over and over the code and I can’t see whats wrong.

Please put me right!

Serban_smile_engaged2_pragsmall
25 Jul 2008, 04:24
Serban Porumbescu (5 posts)

Hey Steve,

I suspect the is the issue is in some other place. It’s strange that item is nil. Sprinkle a few of these (or whatever other messages you see fit) in your code and take a close look at the development.log file:

RAILS_DEFAULT_LOGGER.debug “#{@cart.inspect}”

The debug statements will hopefully give you more insight into the problem.

Tis_me_pragsmall
25 Jul 2008, 12:10
Steve Fairhurst (17 posts)

Hi Serban,

Thanks for the response, I am now going through the log but what would cause the nil object? Throughout the whole tutorial this is the only time I have really come unstuck.

Serban_smile_engaged2_pragsmall
25 Jul 2008, 19:08
Serban Porumbescu (5 posts)

As I look at your message again, the “NoMethodError in Store#add_to_cart” is bothering me. Usually, there is more to that message – something along the lines of “undefined method nameOfMethodIcantFind”.

Maybe some of the info here will help you track the problem down:

http://wiki.rubyonrails.org/rails/pages/NoMethodError

Tis_me_pragsmall
26 Jul 2008, 20:08
Steve Fairhurst (17 posts)

Thanks again Serban, I will have a look on Sunday when I finally get some time to figure it out.

Tis_me_pragsmall
27 Jul 2008, 00:58
Steve Fairhurst (17 posts)

Ok its working again, I changed:

This `

  • <%= item.quantity %> × <%=h item.title %></li> `

    To This `

  • <%=h item.quantity %> × <%=h item.title %></li> `

    And that sorted the problem!

  • Serban_smile_engaged2_pragsmall
    27 Jul 2008, 23:18
    Serban Porumbescu (5 posts)

    Steve, I’m glad to hear that you’ve got things working again. In my opinion, the “h” shouldn’t have fixed anything. Please humor me when you have some time and remove the “h” you added. I suspect your app will still work.

    Tis_me_pragsmall
    28 Jul 2008, 09:41
    Steve Fairhurst (17 posts)

    OK now i’m really confused! You are correct the with or without the “h” it works now but this was the only amendment I made, so why on earth would it just suddenly decide to work!

    Bizarre!

    Thanks for your interest in this Serban, its very much appreciated.

    Generic-user-small
    28 Jul 2008, 11:20
    James West (104 posts)

    because you have re started the server?

    Serban_smile_engaged2_pragsmall
    28 Jul 2008, 11:50
    Serban Porumbescu (5 posts)

    When I replicated your problem (or something close to it) I also ran into a situation where the app continued to misbehave even after I fixed the error I introduced.

    I would need to take a closer look, but I think the session was somehow sticking around in memory (even after clearing them). I say this because the app would continue to fail at its “usual” url localhost:3000/store/add_to_cart/1, but work here 0.0.0.0:3000/store/add_to_cart/1. The localhost url version would work in a new browser (e.g., Firefox instead of Safari). Using a different url or launching in a different browser starts a new session, so again my thought is that the original session was sticking around.

    I should have mentioned this in addition to pointing you to that NoMethodError link, but I hesitated because I wasn’t actually able to get exactly the same error you had. I’ve never run into that sort of problem and had one of those “naw, that couldn’t be it” moments.

    In any case, have fun with the rest of the Depot app and the book :-).

    Serban

    Tis_me_pragsmall
    28 Jul 2008, 16:14
    Steve Fairhurst (17 posts)

    Serban, that makes sense actually I think you may have hit the nail on the head there.

    Thanks again.

    And yes I will enjoy the rest of the book. Cheers!

    You must be logged in to comment