Friday, December 8th, 2006

Working through Agile Web Development with Rails – Part 3




continued in part 4…

.. continued from part 2

Iteration D3: Highlight Changes

This error had me stumped for a while, although it did force me to read through and really understand how the code was working. Something I have found a little difficult in Ruby is the debugging. Coming from a .Net background we have been spoilt with the debugging capabilities of Visual Studio, sometimes to the determent of knowing what is actually going on.

The error occurs when added a new product to the Cart. You get two dialog boxes the first gives “RJS Error: [Object Error]” followed by “Element.update(”cart”, …” this error showed the correctly rendered HTML that was inserted into the cart after clicking ok.

The “Highlight” effect was also missing from the cart item. Reason the <tr> tag for the cart item did not have the id <tr id=”current_item”> However the logic here look sound as the highlight did work for adding additional items of existing products. The problem must therefore be in the Cart.rb file which handles the add_product method.

The line @items << CartItem.new(product) should read @items << current_item = CartItem.new(product)

This assigns the newly created cart item to the current_item variable which is in turn added to the cart items.

  • Anonymous
    I'm still having problems in FireFox, IE, and Safari.


    I noticed the author updated the code so it now reads on two lines:



    current_item = CartItem.new(product)

    @items << current_item



    (I tried it on one line like you wrote but but I still have the error). The cart does get updated but I need to hit F5 to refresh the page. I noticed too in the add_to_cart function of store controller.rb (see below), if I use redirect_to_index then I can click on an image and it adds the product to my cart and renders it properly. However if I use the respond_to { |format| format.js} I get a page in safari that displays a try block with an enormous Element.update call.







    def add_to_cart

    begin

    product = Product.find(params[:id])

    rescue ActiveRecord::RecordNotFound

    logger.error("Attempt to access invalid product #{params[:id]}")

    redirect_to_index("Invalid Product")

    else

    @cart = find_cart

    @current_item = @cart.add_product(product)

    #respond_to { |format| format.js }

    redirect_to_index

    end

    end
  • Stuart
    Thank you so much for this - was having the same problem!
blog comments powered by Disqus