我的电子商务应用中的购物车存在问题。 当用户登录并将项目添加到购物车时,该项目不会立即添加。 用户必须返回并在进入购物车之前再次添加它。
我查看了日志,并且我已经能够发现当用户之前购买了某些东西并且稍后签署购买时会出现问题。下次他们登录购买另一件商品时,是什么时候发生的。
系统会尝试删除旧购物车并创建一个新购物车但最终会发生的情况是,新购物车在首次尝试时显示为空。
有没有办法避免这种情况?
任何帮助将不胜感激。提前谢谢!
class CartsController < ApplicationController
def create
@cart = Cart.new(cart_params)
respond_to do |format|
if @cart.save
format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: @cart }
else
format.html { render :new }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
购物车控制器创建动作
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
belongs_to :user
def add_product(product_id, size)
current_item = line_items.find_by(product_id: product_id, size: size)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id, size: size)
end
return current_item
end
def total_price
line_items.to_a.sum { |item| item.total_price }
end
end
购物车型号
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :invitable,
has_one :cart
after_create :default_cart
def add_products_from_session cart_id
if role == customer?
session_cart = Cart.find cart_id if cart_id
end
if cart && session_cart
session_cart.line_items.each{ |li| self.cart.line_items << li }
session_cart.reload
session_cart.destroy
elsif session_cart
self.cart = session_cart
end
private
def default_cart
self.create_cart
end
end
用户模型
def create
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id, params[:size])
respond_to do |format|
if @line_item.save
format.html { redirect_to customer_cart_path }
format.json { render :show, status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
将产品添加到购物车的代码
来自订单项控制器
cls
$Now = Get-Date
$Days = "21"
$Targetfolder = "c:\TEST"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)
#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $TargetFolder -Include $Extension -Exclude *.xml -Recurse | where { $_.LastWriteTime -le "$LastWrite" }
foreach ($File in $Files) {
if ($File -ne $Null) {
write-host "Deleting File --> $File" -ForegroundColor "Green"
Remove-Item $File.FullName | out-null
}
}
答案 0 :(得分:0)
Cart.create!(user_id: params[:id])
看起来很奇怪。我认为它应该是Cart.create!(user_id: current_user.id)
或current_user.create_cart
。更多的是你必须改变你的代码。我建议:
def show
setup_cart
end
def create
setup_cart
# ... your code
end
protected
def setup_cart
# user logged in
if current_user
# when cart purchased reset existing one
if current_user.cart.purchased_at
session[:cart_id] = nil
current_user.cart = nil
end
# use existing one, marked in session or create a new one
@cart = current_user.cart || Cart.where(id: session[:cart_id]).first || current_user.create_cart
@cart.user = current_user # in case first login
@cart.save!
else # no user logged in
@cart = Cart.where(id: session[:cart_id]).first || Cart.create!
end
session[:cart_id] = @cart.id
end