SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] agile webdev cart error

    hi all,

    i'm using the dollars_to_cents plugin in order to handle the money portion of the shopping cart. i have averything working up until the point where the cart is supposed to list the product a single time with a numericall quantity.

    the error is as follows:

    Code:
    undefined method `quantity' for #<Product:0xb7898ce8>
    the application trace is as follows:

    Code:
    vendor/plugins/dollars_and_cents/lib/dollars_and_cents.rb:48:in `method_missing'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:in `method_missing'
    #{RAILS_ROOT}/app/views/store/add_to_cart.rhtml:4:in `_run_rhtml_store_add_to_cart'
    #{RAILS_ROOT}/app/views/store/add_to_cart.rhtml:3:in `_run_rhtml_store_add_to_cart'
    sample session dump:

    Code:
    - !ruby/object:Product 
        attributes: 
          price_in_cents: "28"
          image_url: /images/image.jpg
          title: New6
          id: "4"
          description: New6
    i've double checked the code (in fact, i copied and pasted it twice). i think my code is right. i'm wondering if using the price_in_cents plugin is the problem. it is mentioned in the first line of the application trace, but i don't know how it might be causign a problem. "price" should be understood by the application.

    cart_item.rb:
    Code:
    class CartItem
      attr_reader :product, :quantity
      def initialize(product)
        @product = product
        @quantity = 1
      end
      def increment_quantity
        @quantity += 1
      end
      def title
        @product.title
      end
      def price
        @product.price * @quantity
      end
    end
    add_to_cart.rhtml
    Code:
    <h1>Your Pragmatic Cart</h1>
    <ul>
      <% for cart_item in @cart.items %>
         <li><%= cart_item.quantity %> &times; <%= h(cart_item.title) %></li>
      <% end %>
    </ul>
    cart.rb
    Code:
    class Cart
      attr_reader :items
      def initialize
        @items = []
      end
      def add_product(product)
        current_item = @items.find {|item| item.product == product}
        if current_item
          current_item.increment_quantity
        else
          @items << CartItem.new(product)
        end
      end
    end
    any guidance is appreciated.

    tia...

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is that the object is a Product. There is no `quantity` method in the product class. It's quite likely that you want to use a CartItem instead of a Product because CartItems do have a `quantity` method.

    Maybe you have an old object in your session? The @cart variable should be a Cart wich contains CartItems, but it could be that there is something wrong with it so it contains Products.

    In this code:

    Code:
    <&#37; for cart_item in @cart.items %>
       <li><%= cart_item.quantity %> &times; <%= h(cart_item.title) %></li>
    <% end %>
    cart_item could be a Product.

    Try to clear your session and see if that resolves the error. If that doesn't work you should check what the classes of @cart and cart_item are:

    Code:
    <%= p @cart %>
    <%= p @cart.items.first %>
    This should output something like:

    Code:
    #<Cart:0x2adb5c0>
    #<CartItem:0x2ad7ae0>

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fenrir2, many thanks. the book goes through *exactly* this scenario and even shows an error page, but i went through that, too. however, i somehow did something to ensure i went through it an unexpected 2nd time.

    time to learn the moral of the story mentioned in agile webdev.

    thanks again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •