Skip to content

Commit d63d61d

Browse files
committed
Fixed Cart and Orders Bugs
1 parent 060bece commit d63d61d

10 files changed

Lines changed: 40 additions & 15 deletions

app/controllers/application_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ class ApplicationController < ActionController::Base
22
protect_from_forgery with: :exception
33
include SessionsHelper
44

5-
before_action :current_cart
5+
before_action :set_cart
66

77
private
88

9-
def current_cart
9+
def set_cart
1010
if session[:cart_id]
1111
cart = Cart.find_by(id: session[:cart_id])
1212
if cart.present?
@@ -17,7 +17,7 @@ def current_cart
1717
end
1818

1919
if session[:cart_id].nil?
20-
@current_cart = Cart.create
20+
@current_cart = Cart.create(:user_id => nil)
2121
session[:cart_id] = @current_cart.id
2222
end
2323
end

app/controllers/carts_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ def destroy
1111
session[:cart_id] = nil
1212
redirect_to root_path
1313
end
14+
15+
private
16+
17+
# Never trust parameters from the scary internet, only allow the white list through.
18+
def cart_params
19+
params.require(:cart).permit(:user_id)
20+
end
1421
end

app/controllers/line_items_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ class LineItemsController < ApplicationController
22
before_action :logged_in_user, only: %i[create destroy add_quantity reduce_quantity]
33

44
def create
5-
# if(@order.nil?)
6-
# @order = Order.new(order_params)
75

86
# Find associated product and current cart
97
chosen_product = Product.find(params[:product_id])
@@ -19,6 +17,7 @@ def create
1917
@line_item = LineItem.new
2018
@line_item.cart = current_cart
2119
@line_item.product = chosen_product
20+
# @line_item.order = Order.first
2221
@line_item.quantity = 1
2322
end
2423

app/controllers/orders_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def create
2323
@order = Order.new(order_params)
2424
@order.update(user_id: @current_user.id)
2525
@current_cart.line_items.each do |item|
26-
@order.line_items << item
2726
item.cart_id = nil
27+
item.order_id = @order.id
28+
item.save
29+
@order.line_items << item
2830
end
2931
@order.save!
3032
Cart.destroy(session[:cart_id])

app/controllers/sessions_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def create
1111
if user.activated?
1212
log_in user
1313
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
14+
15+
1416
# redirect_back_or user
1517
redirect_back_or root_url
1618
else

app/models/cart.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Cart < ApplicationRecord
2-
has_many :line_items, dependent: :destroy
2+
has_many :line_items # , dependent: :destroy
33
has_many :products, through: :line_items
44

55
# LOGIC
66
def sub_total
7-
sum = 0
8-
self.line_items.each do |line_item|
9-
sum+= line_item.total_price
10-
end
11-
return sum
7+
sum = 0
8+
line_items.each do |line_item|
9+
sum += line_item.total_price
10+
end
11+
sum
1212
end
1313
end

app/models/order.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
class Order < ApplicationRecord
22
has_many :line_items, dependent: :destroy
33

4+
# LOGIC
5+
def sub_total
6+
sum = 0
7+
line_items.each do |line_item|
8+
sum += line_item.total_price
9+
end
10+
sum
11+
end
12+
413
def self.search(search)
514
where('name LIKE ?', "%#{search}%")
615
end

app/views/orders/_order.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
</p>
2222
<p>
2323
<h3>Total price:
24-
<%= number_to_currency(@current_cart.sub_total) %></h3>
24+
<%= number_to_currency(order.sub_total) %></h3>
2525
</p>
2626

2727
<button type="button" class="btn btn-success" data-toggle="collapse" data-target="#<%= order.id %>">Show Items</button>
2828
<div id="<%= order.id %>" class="collapse">
2929
<ul class="users">
30-
<% @current_cart.line_items.each do |item| %>
30+
<% order.line_items.each do |item| %>
3131
<%= item.quantity %>
3232
x
3333
<%= render 'shared/order_product' , product:item.product%>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddUserIdOnCarts < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :carts, :user_id, :integer
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20180109134306) do
13+
ActiveRecord::Schema.define(version: 20180114120447) do
1414

1515
create_table "carts", force: :cascade do |t|
1616
t.datetime "created_at", null: false
1717
t.datetime "updated_at", null: false
18+
t.integer "user_id"
1819
end
1920

2021
create_table "line_items", force: :cascade do |t|

0 commit comments

Comments
 (0)