Ruby on rails RubyonRails 2.0.2:在订购后更新产品的数量值

Ruby on rails RubyonRails 2.0.2:在订购后更新产品的数量值,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在使用Instant Rails 2.0,并遵循Rails第三版敏捷Web开发的Depot示例项目 我的问题是:当客户使用购物车和订单进行订单时,我需要更新products表的quantity列。 例如:如果我有10本书(值“10”存储在products表中,带有产品的特定id),而客户想要2本书,那么在我希望我的项目更新可用书的数量值的订单之后,将其减少到8本书 我尝试将其添加到store\u controller.rb中的add\u to\u cart方法中: def add_to_ca

我正在使用Instant Rails 2.0,并遵循Rails第三版敏捷Web开发的Depot示例项目

我的问题是:当客户使用购物车和订单进行订单时,我需要更新products表的quantity列。 例如:如果我有10本书(值“10”存储在products表中,带有产品的特定id),而客户想要2本书,那么在我希望我的项目更新可用书的数量值的订单之后,将其减少到8本书

我尝试将其添加到
store\u controller.rb
中的add\u to\u cart方法中:

def add_to_cart
    product = Product.find(params[:id])
    @quantity = Product.find(params[:quantity])
    @cart = find_cart
    @current_item = @cart.add_product(product)
    @removed = Product.remove_q(@quantity)
     respond_to do |format|
         format.js if request.xhr?
         format.html {redirect_to_index}
     end
    rescue ActiveRecord::RecordNotFound
    logger.error("Product not found #{params[:id]}")
    redirect_to_index("invalid product!")
  end
其中
remove\u q
product.rb
模型的一种方法:

def self.remove_q(quantity)
    @quantity = quantity - 1
  end
当我单击“添加到购物车”按钮时,RoR在控制台中给出错误“未找到产品”。我做错了什么

更新:感谢ipsum的回答。解决方案是在成功订购后减少产品数量。这是
store\u controller.rb的
save\u order
方法:

def save_order
@cart = find_cart
@order = Order.new(params[:order])
@order.add_line_items_from_cart(@cart)
@recipient = 'email@notify.com'
@subject = 'Order'
email = Emailer.create_confirm(@order, @recipient, @subject)
email.set_content_type("text/html")
@cliente = sent
if @order.save
    Emailer.deliver(email)
    return if request.xhr?      
    session[:cart] = nil
    redirect_to_index("Thank you")

else
            render :action => 'checkout'
    end
class Cart
attr_reader :items

 def initialize
   @items = []
 end

 def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
    current_item.increment_quantity

else
       current_item = CartItem.new(product)
   @items << current_item
    end
  current_item
 end

 def total_price
  @items.sum { |item| item.price}
 end

 def total_items
 @items.sum { |item| item.quantity }
 end

 end  
class LineItem < ActiveRecord::Base
 belongs_to :order
 belongs_to :product


 def self.from_cart_item(cart_item)
     li = self.new
     li.product = cart_item.product
     li.quantity = cart_item.quantity
     li.total_price = cart_item.price
     li
 end
end
结束

请注意,
Emailer
是成功订购后通过电子邮件通知的一种模式,购物车由客户添加到购物车中的许多商品组成。成功订购后,如何减少购物车中的产品数量?如何从购物车中提取产品

有一个模型
cart.rb

def save_order
@cart = find_cart
@order = Order.new(params[:order])
@order.add_line_items_from_cart(@cart)
@recipient = 'email@notify.com'
@subject = 'Order'
email = Emailer.create_confirm(@order, @recipient, @subject)
email.set_content_type("text/html")
@cliente = sent
if @order.save
    Emailer.deliver(email)
    return if request.xhr?      
    session[:cart] = nil
    redirect_to_index("Thank you")

else
            render :action => 'checkout'
    end
class Cart
attr_reader :items

 def initialize
   @items = []
 end

 def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
    current_item.increment_quantity

else
       current_item = CartItem.new(product)
   @items << current_item
    end
  current_item
 end

 def total_price
  @items.sum { |item| item.price}
 end

 def total_items
 @items.sum { |item| item.quantity }
 end

 end  
class LineItem < ActiveRecord::Base
 belongs_to :order
 belongs_to :product


 def self.from_cart_item(cart_item)
     li = self.new
     li.product = cart_item.product
     li.quantity = cart_item.quantity
     li.total_price = cart_item.price
     li
 end
end

你试图通过数量来寻找产品。 但是“find”需要一个主键

而不是:

@quantity = Product.find(params[:quantity])
试试这个:

@quantity = product.quantity
更新:

def add_to_cart
    product = Product.find(params[:id])
    @cart = find_cart
    @current_item = @cart.add_product(product)
    product.decrement!(:quantity, params[:quantity])

    respond_to do |format|
      format.js if request.xhr?
      format.html {redirect_to_index}
    end
    rescue ActiveRecord::RecordNotFound
      logger.error("Product not found #{params[:id]}")
      redirect_to_index("invalid product!")
  end

你试图通过数量来寻找产品。 但是“find”需要一个主键

而不是:

@quantity = Product.find(params[:quantity])
试试这个:

@quantity = product.quantity
更新:

def add_to_cart
    product = Product.find(params[:id])
    @cart = find_cart
    @current_item = @cart.add_product(product)
    product.decrement!(:quantity, params[:quantity])

    respond_to do |format|
      format.js if request.xhr?
      format.html {redirect_to_index}
    end
    rescue ActiveRecord::RecordNotFound
      logger.error("Product not found #{params[:id]}")
      redirect_to_index("invalid product!")
  end

你为什么要在2011年学习rails 2.0.2?如果这是你与Rails的第一次接触,也许你应该考虑Rails 3?@ CODIDEN用户,我必须在Rails 2.0.2上做这个项目,因为老师解释了这个旧版本中的示例项目。我知道,听起来很奇怪。你为什么要在2011年学习rails 2.0.2?如果这是你与Rails的第一次接触,也许你应该考虑Rails 3?@ CODIDEN用户,我必须在Rails 2.0.2上做这个项目,因为老师解释了这个旧版本中的示例项目。我知道,听起来很奇怪。@ipsum谢谢你的建议,我在store_controller.rb的“add_to_cart”方法中编辑了这一行。现在我在控制台中没有错误,但是,当我看到产品列表时,在确认订单后,产品的数量没有更新。也许我必须重写model product.rb中的“self.remove_q(quantity)”方法?Cheers@ipsum谢谢,我更新了这个方法,但是我必须编辑行:“product.decrement!(:quantity,params[:quantity]),因为rails在控制台上给了我一个错误:“ArgumentError(错误数量的参数(每1个2个))inDecrement!”。我用这种方式编辑了这一行:“product.decrement!(:quantity)”,看起来很有效!数量已更新。@ipsum我已解决此问题。多亏了你。我还有一个问题。如果我的客户取消会话并希望清空购物车,则store_controller.rb中有一个方法empty_cart:def empty_cart session[:cart]=nil redirect_to_indexend@ipsum如果我想解决反向问题,我的意思是,当客户点击“空车”按钮时,数量需要恢复到原始值。我试图为此插入一个操作,但我怀疑。如何从购物车中提取商品,然后将客户之前添加到购物车中但现在取消的产品数量恢复为原始值?谢谢你的回答。谢谢,您最好在订单成功后减少数量。@ipsum谢谢您的建议,我在store_controller.rb的“add_to_cart”方法中编辑了这一行。现在我在控制台中没有错误,但是,当我看到产品列表时,在确认订单后,产品的数量没有更新。也许我必须重写model product.rb中的“self.remove_q(quantity)”方法?Cheers@ipsum谢谢,我更新了这个方法,但是我必须编辑行:“product.decrement!(:quantity,params[:quantity]),因为rails在控制台上给了我一个错误:“ArgumentError(错误数量的参数(每1个2个))inDecrement!”。我用这种方式编辑了这一行:“product.decrement!(:quantity)”,看起来很有效!数量已更新。@ipsum我已解决此问题。多亏了你。我还有一个问题。如果我的客户取消会话并希望清空购物车,则store_controller.rb中有一个方法empty_cart:def empty_cart session[:cart]=nil redirect_to_indexend@ipsum如果我想解决反向问题,我的意思是,当客户点击“空车”按钮时,数量需要恢复到原始值。我试图为此插入一个操作,但我怀疑。如何从购物车中提取商品,然后将客户之前添加到购物车中但现在取消的产品数量恢复为原始值?谢谢你的回答。谢谢你最好在订单成功后减少数量。