small medium large xlarge

Generic-user-small
23 Jan 2009, 01:57
Gregory Howland (2 posts)

Hi,

I am having some difficulty with adding the ajax effects in iterations D3 and D4. Iteration D2 is working correctly, and I can view ajax effects on various websites so I doubt it is a broswer problem (FF3 on Archlinux).

When I add the D3 and D4 code, no effects occur. Clicking the “Add to Cart” button causes no visible change in store (ie the cart does not update). Only if I reload the page in the browser does the cart update. Are there obvious errors in my code that I am missing?

store_controller.rb

class StoreController < ApplicationController

  def index
    @products = Product.find_products_for_sale
    @cart = find_cart
  end

  # This method must be public because it is called as an action from the store view
  def add_to_cart

    begin # attempt to do something

      # the params object contains parameters from the request button in the view
      # Product.find calls the class method find on the Product database
      product = Product.find(params[:id]) 

    rescue ActiveRecord::RecordNotFound # rescue if a problem occurs

      logger.error("Attempt to access invalid product #{params[:id]}")
      flash[:notice] = "Invalid product"
      redirect_to_index("Invalid Product")
 
    else # Continue if no problem occurs

      @cart = find_cart # This method is in the Controller
      @current_item = @cart.add_product(product) # This method is in the cart model

      respond_to do |format|
        format.js
      end

    end
  end

  def empty_cart
    session[:cart] = nil
    flash[:notice] = "Your cart is currently empty"
    redirect_to_index("Your cart is currently empty")
  end

  # Private methods are not available as an action on the controller
  # (meaning it can't be accessed at /store/find_cart etc)
  private

  def redirect_to_index(msg = nil) #makes default msg nil
    flash[:notice] = msg
    redirect_to(:action => 'index')
  end

  def find_cart
    session[:cart] ||= Cart.new
  end

end

cart.rb

class Cart

# NOTE: this model was not created with script/generate because it is not
# database backed

  attr_reader :items

  def initialize
    # initialize an empty array to hold the items
    @items = []
  end

  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

    return current_item # Return the current item so the controller can use it
  end

  def total_price
    @items.sum{ |item| item.price}
  end

  def total_items
    @items.sum{ |item| item.quantity }
  end

end

_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>

add_to_cart.js.rjs

page.replace_html("cart", :partial => "cart", :object => @cart)

page[:cart].visual_effect :blind_down if @cart.total_items == 1

page[:current_item].visual_effect :highlight,
                                  :startcolor => "#88ff88",
                                  :endcolor => "#114411"
Generic-user-small
06 Feb 2009, 02:29
Gregory Howland (2 posts)

An slight update: I installed the rails debugger and inserted a debuging statement in my add_to_cart.js.rjs after the page.replace_html command. When I investigate page[:cart], an error is thrown saying NoMethodError: undefined method `[]’

I am interpreting this as the html element with id “cart” is not being properly presented by the page hash. I double checked that the cart object has this id, and it does. It is not entirely clear to me at this stage what html elements should and should not be available in the page hash in add_to_cart.js.rjs, but presumably this one should be since I am following the book. This is driving me nuts. Any help would be greatly appreciated.

Generic-user-small
01 Jul 2009, 19:19
Hayden Hancock (3 posts)

I think I am having a similar problem. I on iteration D2. Iteration D1 worked perfectly but during iteration D2 I am getting an RSJ error. When I click “Add to Cart” I get an RSJ error. The error reads:

TypeError: element is null.

I get another popup which looks like the HTML generated code. If I hit refresh, the cart is updated as if the “Add to Cart” button worked. Any help would be much appreciated.

Generic-user-small
01 Jul 2009, 19:28
Hayden Hancock (3 posts)

I fixed my issue. I had to wrap my HTML (_cart.html.erb) in a div with an id of “cart” in order for it to work.

Generic-user-small
10 Sep 2009, 19:29
Robert Audi (3 posts)

I got the same issue and it drives me nuts too. I got the exact same code as the one in the book… Can someone experienced look into that matter, maybe there is a typo in the book..

Generic-user-small
21 Sep 2009, 20:47
Nicolas Noël (8 posts)

Same issue here too. I’m using Mac 10.5 with rails 2.3.4 and ruby 1.8.6 I’m new to rails so I have no idees where to start to look to find a solution. Hope someone can help.

Cheers

Generic-user-small
22 Sep 2009, 10:29
Nicolas Noël (8 posts)

Somehow continuing thru the iteration D4 made it work.

Generic-user-small
25 Oct 2009, 23:08
Dimitri De Frenne (2 posts)

Got a problem with D3 too. Nothing changes until I reload the page. It only works the first time I press the ‘Add to cart’-button.

D4 works, but doesn’t solve D3. Clearing the cache doesn’t work. Restarting the server doesn’t work.

OS: Mac OS X 10.6.1 Rails: 2.3.4 Ruby: ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]

I’ll see if the Rails debugger gets me any further.

You must be logged in to comment