25 Feb 2009, 00:17
Generic-user-small

Charles Curley (3 posts)

I’m a RoR illiterate working my way throught the book. Everything worked until I hit “9.3 Iteration D3: Highlighting Changes”. I applied those changes, and the app broke. The card did not get updated. I found the following in the log:

ActionView::TemplateError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.quantity) on line #6 of app/views/store/_cart_item.html.erb:
3: <% else %>
4:   <tr>
5: <% end %>
6:     <td><%= cart_item.quantity %>&times;</td>
7:     <td><%=h cart_item.title %></td>
8:     <td class="item-price"><%= number_to_currency(cart_item.price) %></td>
9:   </tr>

Which leads me to think that either cart_item.quantity is nil or that cart_item itself is nil. Howver, if cart_item itself were nil, then I suspect the compile would have failed at line 1 instead of line 6.

From _cart_item.html.erb:

<% if cart_item == @current_item %>
  <tr id="current_item">
<% else %>
  <tr>
<% end %>
  <td><%= cart_item.quantity %>&times;</td>
  <td><%=h cart_item.title %></td>
  <td class="item-price"><%= number_to_currency(cart_item.price) %></td>
</tr>

From store_controller.rb:

  def add_to_cart
    product = Product.find(params[:id])
    @cart = find_cart
    @current_item = @cart.add_product(product)
    respond_to do |format|
      format.js
    end
  rescue ActiveRecord::RecordNotFound
    logger.error("Attempt to access invalid product #{params[:id]}" )
    redirect_to_index("Invalid product" )
  end

From cart.rb:

  def add_product(product)
    current_item = @items.find {|item| item.product == product}
    if current_item
      current_item.increment_quantity
    else
      current_item = CartItem.new(product)
      @items << current_item
    end
    current_item
  end

And, finally, from add_to_cart.js.rjs

page.replace_html("cart" , :partial => "cart" , :object => @cart)
page[:current_item].visual_effect :highlight,
                                  :startcolor => "#88ff88" ,
                                  :endcolor => "#114411"

p.s. how about some way to preview this stuff? Thanks

16 Sep 2009, 07:32
Generic-user-small

HoMan Mak (1 post)

I’m having the same problem. Did anyone figured out a solution?

23 Nov 2009, 04:13
Generic-user-small

Brad Wrage (2 posts)

Having same problem..can’t figure it out. Anyone?

21 Dec 2009, 17:39
Generic-user-small

Rob Bertke (1 post)

If it helps anyone, I was having a problem where after D3 everything worked except the highlighting. What caused my problem was that I had left the <tr> starting tag in my _cart_item.html.erb file after the if..else..end block. So I was repeating the <tr> starting tag effectively leaving a blank <tr> with an id=”current_item” and then starting another with no id…which is why the highlighting didn’t work.

I know this isn’t the same exact issue mentioned here but I found this topic when searching for it.

11 Jan 2010, 08:14
Generic-user-small

SETHP (3 posts)

There seems to be another thread on the same subject: http://forums.pragprog.com/forums/66/topics/582

  You must be logged in to comment