Rails购物车 - 在尝试将产品添加到购物车时未尝试实例化(零值)

时间:2018-06-08 15:46:08

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

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

  

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

模特:

OrderItem的

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

1 个答案:

答案 0 :(得分:0)

pry会话中尝试:

@order.errors

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