Я пытаюсь создать систему корзины покупок в своем приложении rails, следуя этому учебнику . Все работало нормально, но когда я пытаюсь добавить продукт (в моем случае это стул), все, что я получаю, это всплывающее окно с надписью Not Valid, терминал не показывает ошибок, кроме Откат отката .
Модель моего продукта
class Chair < ApplicationRecord
mount_uploader :image, ImageUploader
mount_uploader :previewo, PreviewoUploader
mount_uploader :previewt, PreviewtUploader
mount_uploader :previewth, PreviewthUploader
has_many :order_items
validates :description, presence: true, length: { maximum: 600 }
default_scope { where(active: true) }
end
order.rb
class Order < ApplicationRecord
belongs_to :order_status
belongs_to :order_status
has_many :order_items
before_create :set_order_status
before_save :update_subtotal
def subtotal
order_items.collect { |oi| oi.valid? ? (oi.quantity * oi.unit_price) : 0 }.sum
end
private
def set_order_status
self.order_status_id = 1
end
def update_subtotal
self[:subtotal] = subtotal
end
end
order_item.rb
class OrderItem < ApplicationRecord
belongs_to :chair
belongs_to :order
validates :quantity, presence: true, numericality: { only_integer: true, greater_than: 0 }
validate :chair_present
validate :order_present
before_save :finalize
def unit_price
if persisted?
self[:unit_price]
else
chair.price
end
end
def total_price
unit_price * quantity
end
private
def chair_present
if chair.nil?
errors.add(:chair, "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
OrderStatus.rb
class OrderStatus < ApplicationRecord
has_many :orders
end
Клеммный выход
Started POST "/order_items" for 127.0.0.1 at 2018-07-05 22:42:29 +0530
Processing by OrderItemsController#create as JS
Parameters: {"utf8"=>"✓", "order_item"=>{"quantity"=>"1", "chair_id"=>"3"}, "commit"=>"Add to Cart"}
(0.2ms) begin transaction
Chair Load (2.0ms) SELECT "chairs".* FROM "chairs" WHERE "chairs"."active" = ? AND "chairs"."id" = ? LIMIT ? [["active", "t"], ["id", 3], ["LIMIT", 1]]
(0.2ms) rollback transaction
Rendering order_items/create.js.erb
Rendered order_items/create.js.erb (1.2ms)
Completed 200 OK in 77ms (Views: 14.9ms | ActiveRecord: 2.4ms)