Forum: Ruby on Rails Passing quantity in a shopping cart

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Vince W. (Guest)
on 2005-12-15 00:27
I am using this line on my index.rhtml page to capture the quantity
required of a particular item for a basic shopping cart app:

<%= text_field 'line_item', 'quantity', {:size=>3} %> </td>

but my next screen (a cart screen) doesn't reflect what was entered in
that text field when the item is added to the cart. That is,
<td><%= item.quantity %></td>

shows 0 no matter what is entered on index.rhtml (I removed the code
that was setting it to 1 in the line_item model.)

How should I pass the quantity the client wants to order to the shopping
cart?

My db table is named line_items and has an integer field named
quantity.. this is basically an exercise to extend the cart app in Agile

TIA..
Hunter's Lists (Guest)
on 2005-12-15 01:33
(Received via mailing list)
What does your code look like that populates the line items from the
form
data?
Vince W. (Guest)
on 2005-12-15 01:42
Hunter's Lists wrote:
> What does your code look like that populates the line items from the
> form
> data?

class LineItem < ActiveRecord::Base

  belongs_to :product
  belongs_to :order
   def self.for_product(product)
    item = self.new
    item.product = product
    item.unit_price = product.price
    item
   end
end


This is the controller for the line_item.. I couldn't define
item.quantity since quantity is already a field in the line_items table.
Maybe I've got quantity in the wrong table?  My other tables are
products, orders, and users.  There is also a qty_available in products
but that reflects stock level and not the quantity that is being
ordered.
Hunter's Lists (Guest)
on 2005-12-15 02:18
(Received via mailing list)
This looks like the model, not the controller.

You should have code in your controller that basically takes the stuff
from
the form and puts it in an instance of LineItem.
Vince W. (Guest)
on 2005-12-15 06:18
Hunter's Lists wrote:
> This looks like the model, not the controller.
>
> You should have code in your controller that basically takes the stuff
> from
> the form and puts it in an instance of LineItem.

The code in my controller looks like this:

def add_to_cart
  product = Product.find(params[:id])
  @cart = find_cart
  @cart.add_product(product)
  redirect_to(:action => 'display_cart')
rescue
  logger.error("Attempt to access invalid product #{params[:id]}")
  redirect_to_index('Invalid Product')
end


Would I add a line that says @cart.add_quantity(line_item) ?
Hunter's Lists (Guest)
on 2005-12-15 06:31
(Received via mailing list)
This code looks up a product based on the passed in id, finds the cart,
adds
the product that it found to the cart and redirects.

Nowhere in this code is it pulling anything from any form (except for
the
id).

Based on this, it seems strange that this would be fired by submitting a
form. This looks like an action that would fire attached to a hyperlink
on a
screen listing products, to add them to your cart.
Vince W. (Guest)
on 2005-12-15 23:44
Hunter's Lists wrote:
> This code looks up a product based on the passed in id, finds the cart,
> adds
> the product that it found to the cart and redirects.
>
> Nowhere in this code is it pulling anything from any form (except for
> the
> id).
>
> Based on this, it seems strange that this would be fired by submitting a
> form. This looks like an action that would fire attached to a hyperlink
> on a
> screen listing products, to add them to your cart.

Sorry but I am very new to Ruby and OO programming general.  I looked up
forms and it said that it could be called or just embedded like I have
it in my index.rhtml file: <%= text_field 'line_item', 'quantity',
{:size=>3} %> </td>

With that, I am guessing I need to add a model to my cart.rb that
defines a method for add_quantity and then call that method in my store
controller at the same time I call the add_to_cart since that only adds
products at this point.

Does that sound right or am I way off?
Shawn M. (Guest)
on 2005-12-15 23:51
(Received via mailing list)
Vince,

I exetended the line_item example like this:
the important thing is the qty=1 in the self.for_product definition.
when you call it you do this:
  shipment <<
ShipmentItem.for_product(@product,@params[:lineitem]["quantity"])

or whatever variation works for you.

Shawn


class ShipmentItem < ActiveRecord::Base
    set_table_name "transaction_items"

    belongs_to :product
    belongs_to :shipment, :foreign_key => "shipment_id", :dependent =>
true
    acts_as_list :scope => :shipment


    def self.for_product(product,qty=1)
        item = self.new
        item.sku = product.sku
        item.quantity = qty
        item.product = product
        item.unit_price = product[price_field]
        item.unit_price = product.price
        item.recalc_subtotal
        item
    end

    def quantity=(value)
        write_attribute("quantity",value)
        self.recalc_subtotal
    end

    def price=(value)
        write_attribute("price",value)
        self.recalc_subtotal
    end

    def recalc_subtotal
        if @qty.nil? or @unit_price.nil?
            write_attribute(:subtotal,0)
        else
            write_attribute(:subtotal,@qty * @unit_price)
        end
    end


end
Shawn M. (Guest)
on 2005-12-15 23:54
(Received via mailing list)
I made a boo-boo, ignore that line that says item.unit_price =
product[price_field], just remove it ;)

Thats a little something I'm working on for custom prices for
different customer types.

Shawn
Shawn M. (Guest)
on 2005-12-15 23:57
(Received via mailing list)
Ok, so maybe I should proof read code that I'm working on before I post
it.
come on, flame me you know you want to. ;)

Corrected Code Here:

class ShipmentItem < ActiveRecord::Base
    set_table_name "transaction_items"

    belongs_to :product
    belongs_to :shipment, :foreign_key => "shipment_id", :dependent =>
true
    acts_as_list :scope => :shipment

    def self.for_product(product,qty=1,price_field="price")
        item = self.new
        item.sku = product.sku
        item.quantity = qty
        item.product = product
        item.unit_price = product.price
        item.recalc_subtotal
        item
    end

    def quantity=(value)
        write_attribute("quantity",value)
        self.recalc_subtotal
    end

    def price=(value)
        write_attribute("unit_price",value)
        self.recalc_subtotal
    end

    def recalc_subtotal
        if @quantity.nil? or @unit_price.nil?
            write_attribute(:subtotal,0)
        else
            write_attribute(:subtotal,@quantity * @unit_price)
        end
    end


end
This topic is locked and can not be replied to.