开发者

Ruby On Rails 2.0.2: Update quantity value of a product after order

开发者 https://www.devze.com 2023-02-23 23:24 出处:网络
I\'m using Instant-Rails 2.0 and following the Depot example project of Agile Web Development with Rails 3rd edition.

I'm using Instant-Rails 2.0 and following the Depot example project of Agile Web Development with Rails 3rd edition.

My question is: When a customer makes an开发者_C百科 order, with the cart and the order form, I need the update the column quantity of products table. An example: If I have 10 books (the value "10" is stored in products table with the specific id of the product) and the customer wants 2 books, after the order I want that my project updates the quantity value of available books, decrement it to 8 books.

I tried to add that in store_controller.rb, in the add_to_cart method:

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

Where remove_q is a method of product.rb model:

def self.remove_q(quantity)
    @quantity = quantity - 1
  end

RoR gives me the error "product not found" in the console when I click in the "add to cart" button. What am I doing wrong?

UPDATE: Thanks to ipsum for answer. The solution is to decrement the quantities of products after successful order. This is the method save_order of store_controller.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

end

Please note that Emailer is a model for notification via email after successful order, the cart is made from many line_items that are the products customer adds to cart. How can I decrement the quantities of products in cart after successful order? How can I extract products from cart?

There is the model cart.rb:

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  

and the model line_item.rb:

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


You try to find a product through the quantity. but "find" expects a primary key

Instead of:

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

try this:

@quantity = product.quantity

UPDATE:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消