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.
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} %>
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.
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.
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”