[英]Rails: ActiveRecord::RecordNotFound in Cart
I user friendly_id for all the items. 我对所有项目都使用friendly_id。 I am getting an error in the cart.rb model saying ..
Couldn't find Product with 'id'=the-pen
我在cart.rb模型中
Couldn't find Product with 'id'=the-pen
一个错误,说.. Couldn't find Product with 'id'=the-pen
I'm unsure what to do. 我不确定该怎么办。 Any help would be appreciated
任何帮助,将不胜感激
cart.rb cart.rb
class CartItem < ActiveRecord::Base
attr_reader :product_id, :quantity
def initialize product_id, quantity = 1
@product_id = product_id
@quantity = quantity
end
def increment
@quantity = @quantity + 1
end
def product
Product.find product_id
end
def total_price
product.price * quantity
end
end
ApplicationController.rb ApplicationController.rb
def initialize_cart
@cart = Cart.build_from_hash session
end
OrderForm.rb OrderForm.rb
def build_order_items
@cart.items.each do |item|
@order.order_items.create! product_id: item.product_id, quantity: item.quantity
end
Your CartItem's product
method still uses the .find
method by itself, which may be throwing the error as you're inputting a string as an argument instead of the numeric ID. CartItem的
product
方法仍.find
使用.find
方法,当您输入字符串作为参数而不是数字ID时,该方法可能会.find
错误。 Try modifying it to use .friendly.find
instead. 尝试将其修改为使用
.friendly.find
。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.