【问题标题】:Rails shopping cart - order not instantiated when trying to add products to cart (nil value)Rails 购物车 - 尝试将产品添加到购物车时未实例化订单(零值)
【发布时间】:2023-03-20 03:41:01
【问题描述】:

我正在尝试构建一个带有基本购物车的 rails(带有 devise gem)电子商店应用程序。我在互联网上找到了一些处理购物车的教程,并且我已经按照它进行操作。
代码与下面的帖子非常相似:

Rails Shopping Cart - not adding to current order

但是,我在尝试将产品添加到购物车时遇到了订单部分的问题。订单从未实例化(我假设),因此无法保存,产品也不会添加到购物车中。
我做了很多研究并尝试调试,但我被卡住了。

抱歉,这篇文章太长了,但我试图尽可能准确,以简化答案。

你能帮忙找出我哪里错了吗?
提前谢谢您的回答。


控制器:

Order_items

class OrderItemsController < ApplicationController
  skip_before_action :authenticate_user!

  def create
    @order = current_order
    @item = @order.order_items.new(order_item_params)
    if @order.save
      session[:order_id] = @order.id
      flash[:notice] = "Product Successfully added to your cart"
      redirect_to produits_path
    else
      flash[:notice] = "Problem"
      redirect_to produits_path
    end
  end

  def update
    @order = current_order
    @order_item = @order.order_items.find(params[:id])
    @order_item.update_attributes(order_item_params)
    @order_items = @order.order_items
  end

  def destroy
    @order = current_order
    @order_item = @order.order_items.find(params[:id])
    @order_item.destroy
    @order_items = @order.order_items
  end

private

  def order_item_params
    params.require(:order_item).permit(:quantity, :produit_id)
  end

end

购物车

class CartsController < ApplicationController
  skip_before_action :authenticate_user!

  def show
    @order_items = current_order.order_items
  end
end

产品

class ProduitsController < ApplicationController
  skip_before_action :authenticate_user!

  def index
    @produits = Produit.all
    @order_item = current_order.order_items.new
  end
end

应用程序

 class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  helper_method :current_order

  before_action :authenticate_user!
  before_action :configure_permitted_parameters, if: :devise_controller?

  def current_order
    if !session[:order_id].nil?
      Order.find(session[:order_id])
    else
      Order.new
    end
  end

  def configure_permitted_parameters
    # For additional fields in app/views/devise/registrations/new.html.erb
    devise_parameter_sanitizer.permit(:sign_up, keys: [:prenom, :nom, :adresse, :telephone])
    devise_parameter_sanitizer.permit(:account_update, keys: [:prenom, :nom, :adresse, :telephone])
  end
end

型号:

订单项

class OrderItem < ApplicationRecord
  belongs_to :order
  belongs_to :produit

  validates :quantity, presence: true, numericality: { only_integer: true, greater_than: 0 }
  validate :product_present
  validate :order_present

  before_save :finalize

  def unit_price
    if persisted?
      self[:unit_price]
    else
      produit.prix
    end
  end

  def total_price
    unit_price * quantity
  end

private

  def product_present
    if produit.nil?
      errors.add(:produit, "is not valid or is not active.")
    end
  end

  def order_present
    if order.nil?
      errors.add(:order, "is not a valid order.")
    end
  end

  def finalize
    self[:unit_price] = unit_price
    self[:total_price] = quantity * self[:unit_price]
  end
end

订购

class Order < ApplicationRecord
  belongs_to :order_status
  belongs_to :user
  has_many :order_items
  before_create :set_order_status
  before_save :update_subtotal

  def subtotal
    order_items.collect { |oi| oi.valid? ? (oi.quantity * oi.prix) : 0 }.sum
  end


private
  def set_order_status
    self.order_status_id = 1
  end

  def update_subtotal
    self[:subtotal] = subtotal
  end
end

调试时,我看到项目变量 ok(存在产品 id 和数量)但订单仍然“nil”,所以我输入代码的 else 部分。

     4: def create
     5:   binding.pry
     6:   @order = current_order
     7:   @item = @order.order_items.new(order_item_params)
 =>  8:   if @order.save
     9:     session[:order_id] = @order.id
    10:     flash[:notice] = "Product Successfully added to your cart"
    11:     redirect_to produits_path
    12:   else
    13:     flash[:notice] = "Problem"
    14:     redirect_to produits_path
    15:   end
    16: end

[4] pry(#<OrderItemsController>)> @order
=> #<Order:0x00007f8714ed1a50
 id: nil,
 status: nil,
 total_price: nil,
 created_at: nil,
 updated_at: nil,
 subtotal: nil,
 shipping: nil,
 user_id: nil>
[5] pry(#<OrderItemsController>)> @item
=> #<OrderItem:0x00007f8718e684e8 id: nil, produit_id: 47, order_id: nil, quantity: 1, created_at: nil, updated_at: nil>


     4: def create
     5:   binding.pry
     6:   @order = current_order
     7:   @item = @order.order_items.new(order_item_params)
     8:   if @order.save
     9:     session[:order_id] = @order.id
    10:     flash[:notice] = "Product Successfully added to your cart"
    11:     redirect_to produits_path
    12:   else
 => 13:     flash[:notice] = "Problem"
    14:     redirect_to produits_path
    15:   end
    16: end


pry(#<OrderItemsController>)> @order.save
   (0.3ms)  BEGIN
  ↳ (pry):7
   (0.3ms)  ROLLBACK
  ↳ (pry):7
=> false

【问题讨论】:

  • update error was in the callbabk syntax in Order model before_validation :set_order_status, on: :create 正确设置订单状态。我还删除了与用户的链接,这是不必要的,因为我不希望访问者连接到创建购物车

标签: ruby-on-rails ruby session e-commerce cart


【解决方案1】:

pry 会话中尝试:

@order.errors

查看哪些验证失败,从而阻止 @order 被保存。

【讨论】:

  • 感谢您的提示。实际上,我对所需的 user_id 有疑问。这已解决,但现在问题出在未设置的 order_status 上。我会在这里调查一下
猜你喜欢
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多