尝试将产品添加到卡中时,出现错误“除非保存了父项,否则无法调用create”。

时间:2019-02-25 17:43:47

标签: ruby-on-rails

我遇到了一个错误:“除非保存了父级,否则您不能调用create”,我无法将产品添加到购物车。

该行中的错误是“ order.line_items.create product:product”

我不知道怎么了。

请帮助,也许您会看到一些东西。 预先感谢您的帮助。

helper方法

protect_from_forgery with: :exception
helper_method :current_cart    
def current_cart
 if session[:order_id]
   Order.find(session[:order_id])
 else
   Order.new
 end
end

def current_cart_or_create
 c = current_cart
 if c.new_record?
   c.save
   session[:order_id] = c.id
 end
 c
end

购物车控制器

  class CartController < ApplicationController
def show
  @cart = current_cart
end

def edit
  @cart = current_cart
  @cart.build_address if @cart.address.blank?
end

def update
  @cart = current_cart
  if @cart.update_attributes(cart_attributes)
    @cart.update_attribute(:shipping_cost, @cart.shipping_type.cost)
    redirect_to confirmation_cart_path
  else
    render action: :edit
  end
end

def confirmation
  @cart = current_cart
end

def finish
  @cart = current_cart
  @cart.transition_to :confirmed
  session.delete(:order_id)
  flash[:notice] = "Dziękujemy za zamówienie!"
  redirect_to root_path
end

def add_product
  order = current_cart_or_create
  product = Product.find(params[:product_id])
  if item = order.line_items.where(product: product).first
    item.quantity += 1
    item.save
  else
    order.line_items.create product: product,
    quantity: 1,
    unit_price: product.price,
    item_name: product.name
  end
  redirect_to :back, notice: "Dodano produkt do koszyka"
  #redirect_back(fallback_location: root_path)
end

def remove_product
  order = current_cart
  product = Product.find(params[:product_id])
  item = order.line_items.where(product: product).first
  if item
    item.destroy
  end
  redirect_to :back, notice: "Usunięto produkt z koszyka"
end

private

def cart_attributes
  params.require(:order).permit(
    :shipping_type_id,
    :comment,
    :address_attributes => [
      :first_name,
      :last_name,
      :city,
      :zip_code,
      :street,
      :email
    ]
  )
end

end

2 个答案:

答案 0 :(得分:0)

您的Order.new很可能没有通过您的c.save保存,因为它没有通过您的Order模型的验证。

您的日志可能显示类似以下内容:

  (4.8ms)  ROLLBACK
=> false

当它尝试执行c.save时。 您应该尝试使用c.save!代替,然后日志应该显示详细的错误,例如: ActiveRecord::RecordInvalid: Validation failed: Name can't be blank... 或者只是检查您的订单模型以查看哪些字段是必填字段(验证)。

然后填写必填字段,例如:

o = Order.new
o.name = "new order"
o

然后,您应该能够保存此新的父记录并创建关联的子模型(LineItems)的记录

答案 1 :(得分:0)

好的,我将c.save更改为c.save!并收到此错误“否定验证:运输类型必须存在导轨” 添加“ optional:true”后,一切正常,但是我需要shipping_type是必需的,而不是可选的,我应该如何纠正呢?

订单类

  class Order < ActiveRecord::Base
include Statesman::Adapters::ActiveRecordQueries
belongs_to :shipping_type, optional: true
has_many :line_items
has_one :address
has_many :transitions, class_name: "OrderTransition", autosave: false

accepts_nested_attributes_for :address

delegate :can_transition_to?, :transition_to!, :transition_to, :current_state,
  to: :state_machine

def state_machine
  @state_machine ||= OrderStateMachine.new(self, transition_class: OrderTransition,
                                           association_name: :transitions)
end

def full_cost
  line_items.map { |e| e.full_price }.sum + shipping_cost
end

def self.transition_class
  OrderTransition
end

def self.initial_state
  OrderStateMachine.initial_state
end

def self.transition_name
  :transitions
end
end