Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4333128
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:21:03+00:00 2026-05-21T10:21:03+00:00

I’m using Instant-Rails 2.0 and following the Depot example project of Agile Web Development

  • 0

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 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:21:04+00:00Added an answer on May 21, 2026 at 10:21 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.