Skip to content
Browse files

Trying to save shopping cart line items to orders

  • Loading branch information...
1 parent 5c73be8 commit 2da50ed0cf6720e4f4e7f345a351da4cee44c3c1 @florida committed
View
8 app/controllers/store_controller.rb
@@ -5,6 +5,7 @@ def index
def add_to_cart
product = Product.find(params[:id])
+ get_order
@cart = get_cart
@cart.add_line_item(product)
@@ -40,7 +41,8 @@ def checkout
if @items.empty?
redirect_to root_path("There's nothing in your cart")
else
- @order = Order.new
+ @cart.empty_cart_contents!
+ @order = @cart.get_order
end
end
@@ -50,6 +52,8 @@ def get_cart
session[:cart] ||= Cart.new
end
-
+ def get_order
+ session[:order] ||= Order.new
+ end
end
View
6 app/models/cart.rb
@@ -10,6 +10,7 @@ def initialize
@items = []
@total_price = 0.0
@taxes = 0.0
+ @order = session[:order]
end
@@ -21,7 +22,8 @@ def add_line_item(product)
if item
item.quantity += 1
else
- item = Lineitem.add_product(product)
+ item = Lineitem.add_product(product, @order)
+
@items << item
end
@@ -33,5 +35,7 @@ def empty_cart_contents!
@items = []
@total_price = 0.0
@taxes = 0.0
+ @order = Order.new
end
+
end
View
4 app/models/lineitem.rb
@@ -5,8 +5,8 @@ class Lineitem < ActiveRecord::Base
# # # # next feature, receiving quantity...
- def self.add_product(product)
- item = self.new
+ def self.add_product(product, order)
+ item = order.lineitems.build
item.quantity = 1
item.product = product
#record the price during the sale
View
2 app/views/store/checkout.html.erb
@@ -2,7 +2,7 @@
<%= render :partial => 'items_in_cart', :collection => @items, :as => 'item' %>
-
+
<%= form_for [:admin, @order] do |f| %>
<section class="field">
<%= f.label :first_name %>
View
1 config/environment.rb
@@ -3,3 +3,4 @@
# Initialize the rails application
Elmorfstore::Application.initialize!
+config.action_controller.session_store = :active_record_store

0 comments on commit 2da50ed

Please sign in to comment.
Something went wrong with that request. Please try again.